split
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
***************************
Trailing empty strings are therefore not included in the resulting array.
public String[] split(String regex)
Splits this string around matches of the given regular expression.
This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.
***************************
Trailing empty strings are therefore not included in the resulting array.
遇到一个“$”则不管,当遇到“$$”的时候就输出一个空格!
1,StringTokenizer st = new StringTokenizer(source,"$", true);
2,增加boolean flag = false;用来判断两个连续的“$”
String source ="2005$02$28$$Monday$true$false";
StringTokenizer st = new StringTokenizer(source,"$", true);boolean flag = false;
while (st.hasMoreElements()){
String myString = st.nextToken();
if (myString.equals("$")){
if (flag){
System.out.println(" ");
}
flag = true;
continue;
}
System.out.println(myString);
flag = false;
}
例子如下:
import java.lang.*;
import java.lang.String;
public class string
{
public static void main (String [] args)
{
String source ="2005$02$28$$Monday$√$×";
String source1;
source1=source.replace('$','j');
System.out.println(source1);
String source2;
source2=source1.replaceAll("jj"," ");
System.out.print(source2);}
}
空串的时候不被记录的下来的
可以分割特殊字符
public static String[] split(String input, String separator, int limit) {
int index = 0;
boolean matchLimited = limit > 0;
ArrayList matchList = new ArrayList();
// Matcher m = new Matcher(this, input);
Pattern p=Pattern.compile(separator);
Matcher m=p.matcher(input);
// Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index, m.start()).toString();
matchList.add(match);
index = m.end();
} else if (matchList.size() == limit - 1) { // last one
String match = input.subSequence(index,
input.length()).toString();
matchList.add(match);
index = m.end();
}
} // If no match was found, return this
if (index == 0)
return new String[] {input.toString()}; // Add remaining segment
if (!matchLimited || matchList.size() < limit)
matchList.add(input.subSequence(index, input.length()).toString()); // Construct result
int resultSize = matchList.size();
// if (limit == 0)
// while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
// resultSize--;
String[] result = new String[resultSize];
return (String[])matchList.subList(0, resultSize).toArray(result);
}String[] ary = test.split("\\,");换成:
String[] ary = split(test, "\\,", 0);
就可以了.
// while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
// resultSize--;
这段程序加上后就是原先那种效果.
我写的方法如下:
private String[] split(String original,String regex){
ArrayList ary = new ArrayList();
String vlu = null;
while(true){
if(original.indexOf(regex) != -1){
vlu = original.substring(0, original.indexOf(regex));
ary.add(vlu);
original = original.substring(original.indexOf(regex) + 1);
}else{
ary.add(original);
break;
}
}
return (String[])ary.toArray(new String[0]);
}