-
Notifications
You must be signed in to change notification settings - Fork 2
/
T.java
256 lines (230 loc) · 7.02 KB
/
T.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package qgb;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.util.ArrayList;
/**modified at 2014-09-22 21:21:51**/
public class T {
public static void main(String[] args) throws IOException {
//U.print("[%s]",lineWrap("0123456789Abcdefghijk",3));
//U.print("0!!!!34567!!!!!!89A2!!!".length());
String stW = "0!!!!34567!!!!!!89A2!!!!bcdefghijk!!";
//stW=QText.delChars(stW,'|','`','~','!','@','#','$','%','^','&','*','(',')');
U.print(replaceAll(stW, "!!", "!!!"));
U.print(padNum(1,9999));
U.print(sub("123456","23"));
U.print(sub("123456","23","56"));
//U.print(delMutiChar(stW));
// U.print(str.substring(0,str.indexOf("3") ));
}
public static final String gsWinNewline="\r\n";
/**format Decimal
* 2014-7---**/
public static String formatNum(Object ao, String ast){
DecimalFormat df = new DecimalFormat(ast);
// U.print( df.format(ao));
return df.format(ao);
}
// /////////////////////////////////////////////////
public static String repeat(int itimes, String ast_text) {
if (itimes < 1) {
return "";
}
StringBuilder sb=new StringBuilder();
for (int i = 0; i < itimes; i++) {
sb.append(ast_text) ;
}
return sb.toString();
}
public static String delMutiChar(String ast_text) {
String ta = "";
String tb = "";
for (int i = 0; i < ast_text.length(); i++) {
tb = ast_text.substring(i, i + 1);
if (ta.contains(tb) == false) {
ta = ta + tb;
}
}
return ta;
}
public static String padHead(Object aBePaded, int aiMax, String astPad) {
int ia = aiMax - aBePaded.toString().length();
if (ia < 0) {
U.notify("aBePaded too large!");
return aBePaded.toString();
}
int im = ia / astPad.length();
int ir = ia % astPad.length();
return (repeat(ir, " ") + repeat(im, astPad) + aBePaded.toString());
}
/**Result length is aiMax*/
public static String pad(Object aBePaded, int aiMax, String astPad) {
int ia = aiMax - aBePaded.toString().length();
if (ia < 0) {
U.notify("aBePaded too large!");
return aBePaded.toString();
}
int im = ia / astPad.length();
int ir = ia % astPad.length();
// System.out.printf("ai=%d\nia=%d\nim=%d\nast.l=%d\nir=%d\n",
// ai,ia,im,astPad.length(),ir);
return (aBePaded.toString() + repeat(im, astPad) + repeat(ir, " "));
}
public static String pad(Object aBePaded, int aiMax) {
int ia = aiMax - aBePaded.toString().length();
if (ia < 0) {
U.notify("aBePaded too large!");
return aBePaded.toString();
}
// System.out.printf("ai=%d\nia=%d\nim=%d\nast.l=%d\nir=%d\n",
// ai,ia,im,astPad.length(),ir);
return (aBePaded.toString() + repeat(ia, " "));
}
/**�����������Խ���쳣**/
public static String getBegins(String ast, int ai) {
return (String) ast.subSequence(0, U.min(ast.length(),ai));
}
/**�����ڲ���SQL��䣻
* 2014-09-21 00:03:01**/
public static String format(String format, Object... args) {
ByteArrayOutputStream byos=new ByteArrayOutputStream();
PrintStream ps=new PrintStream(byos,true);
ps.format(format, args);
try {
return byos.toString(CharsetName.GST_DEF);
} catch (UnsupportedEncodingException e) {e.printStackTrace();}
return "";
}
public static String lineWrap(StringBuilder asb,int aiColumns) {
if(asb==null)return null;
if(aiColumns<1||asb.length()<=aiColumns)return asb.toString();
int ir=asb.length(),ic=aiColumns;//,irows=imax/aiColumns,ir=imax%aiColumns+irows;
//if((ir)>aiColumns)irows+=ir/aiColumns
while (ir>aiColumns) {
asb.insert(ic,'\n');
ic+=aiColumns+1;
ir-=(aiColumns);
//U.print("r=%d,c=%d",ir,ic);
}
//if(ir>0)asb.insert(ic,'\n');
return asb.toString();
}
/**eclipse����̨���76����ͨ��,96��ȫ����**/
public static String lineWrap(String ast,int aiColumns) {
return lineWrap(new StringBuilder(ast), aiColumns);
}
public static String delChars(String stW, String asyc) {
return delChars(stW, asyc.toCharArray());
}
public static String delChars(String stW, char... ayc) {
if (ayc.length<1||stW.length()<1)return stW;
StringBuilder sb=new StringBuilder(stW);
int idc=0,imax= sb.length();
for (int i = 0; i <imax; i++) {
char c=sb.charAt(i-idc);
for (int j = 0; j < ayc.length; j++) {
if(c==ayc[j]){
sb.deleteCharAt(i-idc);
idc++;
break;
}
}
}
return sb.toString();
}
/**�ݹ��滻��ֱ�������� astOld</br>
* **/
public static String replaceAll(String ast, String astOld, String astNew) {
/**��astNew �а� astOldʱ,���������ݹ顣���Է��� �滻һ�κ��ֵ��*/
if (astNew.contains(astOld)) {
return ast.replace(astOld, astNew);
}
if (ast.contains(astOld)==false) {
return ast;
}
return replaceAll(ast.replace(astOld, astNew), astOld, astNew);
}
//////////////////////////////////////////////////////////////
public static String replaceChars(String ast,String astNew, String asyc) {
return replaceCharArray(ast,astNew,asyc.toCharArray());
}
public static String replaceChars(String ast,String astNew, char... ayc) {
return replaceCharArray(ast,astNew,ayc);
}
public static String replaceCharArray(String ast,String astNew, char[] ayc) {
if (ayc==null||ayc.length<1)return ast;
ayc=Y.delMuti(ayc);
if (ayc.length<1||ast==null||ast.length()<1)return ast;
if (astNew==null||astNew.length()<1)return delChars(ast, ayc);
StringBuilder sb=new StringBuilder(ast);
int idc=0,imax= sb.length();
final int iNew=astNew.length()-1;
for (int i = 0; i <imax; i++) {
char c = sb.charAt(i-idc);
for (int j = 0; j < ayc.length; j++) {
if(c==ayc[j]){
sb.replace(i-idc,i-idc+1, astNew);
idc=idc+iNew;
break;
}
}
}
return sb.toString();
}
public static String padNum(int i, int MaxValue) {
final int im= U.get_intDigit(MaxValue);
if (i>Math.pow(10, im)) {
return (i+"").substring(0,im);
}
return i+repeat(im-(i+"").length(), " ");
}
public static String subLast(String ast, String as1) {
if (ast==null||as1==null) {
return "";
}
if (!ast.contains(as1)) {
return "";
}
int i=ast.lastIndexOf(as1)+as1.length();
if (i<0||i>=ast.length()-1) {
return "";
}
return ast.substring(i);
}
public static String sub(String ast, String as1) {
if (ast==null||as1==null) {
return "";
}
if (!ast.contains(as1)) {
return "";
}
int i=ast.indexOf(as1)+as1.length();
return ast.substring(i);
}
public static String sub(String ast, String as1, String as2) {
if (ast==null||as1==null||as2==null) {
return "";
}
if (!ast.contains(as1)||!ast.contains(as2)) {
return "";
}
int ia=ast.indexOf(as1)+as1.length();
int ib=ast.indexOf(as2, ia);
if (ib<=ia) {
return "";
}
return ast.substring(ia,ib);
}
public static String replace(String ast, String as1, String as2) {
if(ast==null||as1==null||as2==null||ast.equals("")){
return "";
}
if(as1.equals("")){
return ast;
}
return ast.replace(as1, as2);
}
}