String s = "ab#cd#ef#gh#";
String regex = "#";
char [] sChar = s.toCharArray();
char [] regexChar = regex.toCharArray();
int count = 1;
for (int i = 0; i < regexChar.length; i++){
for (int j = 0; j < sChar.length; j++){
if (regexChar [i] == sChar[j]){
count += 2;
}
}
}
String [] splitS = new String [count];
String temp;
for (int k = 0; k < count; k++){
temp = "";
for (int i = 0; i < regexChar.length; i++){
for (int j = 0; j < sChar.length; j++){
if (regexChar[i] == sChar[j]){
temp = Character.toString(regexChar[i]);
}
else {
temp = temp + Character.toString(sChar[j]);
}
}
}
splitS[k] = temp;
}
這是我的代碼,但它給了我splitS[#,#,#,#,#,#,#,#]
。但它應該是splitS[ab,#,cd,#,ef,#,gh,#]
。誰能告訴我爲什麼這樣做?不使用String.split()分割一個字符串 - 並返回分隔符
by regexChar你的意思是分割? – damienix 2014-10-01 20:33:14
'regex'會比1個字符長嗎? – jedwards 2014-10-01 20:33:35
爲什麼不使用'StringTokenizer'? – 2014-10-01 20:33:55