问一个String的split的问题还没有人问过!
String test = "1,,";
String[] ary = test.split("\\,");
期望得到的ary 是{1, "" , ""}但是得不到,
请问如何做到?多谢大虾!
问题点数:20、回复次数:12Top
1 楼adenzhang(佐敦)回复于 2005-04-05 10:14:12 得分 0
怎么我问的问题都没人回答?!Top
2 楼faen(发恩)回复于 2005-04-05 10:51:36 得分 0
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.
Top
3 楼bevin1010(木瓜)回复于 2005-04-05 10:55:39 得分 0
楼上已经说清楚了,空串是得不到的,想别的办法吧Top
4 楼musicer(musicer)回复于 2005-04-05 11:03:48 得分 0
没办法,只有逐个扫描了Top
5 楼chg2008(岂能尽如人意,但求无愧我心!)回复于 2005-04-05 11:07:49 得分 0
用StringTokenizerTop
6 楼OnlyFor_love(『勾勾手指头 一辈子不分手』)回复于 2005-04-05 11:12:15 得分 0
split只能拆分普通的分隔符,要想拆分特殊的分隔符(比如$,//,@),那你就要用StringTokenizer
我给你一个例子,你好好看看,就会明白了!
题目要求:有一个字符串,里面有类似于分割符的“$”,现在要求输出字符串,
遇到一个“$”则不管,当遇到“$$”的时候就输出一个空格!
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;
}Top
7 楼OnlyFor_love(『勾勾手指头 一辈子不分手』)回复于 2005-04-05 11:16:42 得分 0
或者更简单的方法就是,先把特殊分隔符用别的代替!
例子如下:
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);
}
}
Top
8 楼topil(认认真真学习,塌塌实实工作)回复于 2005-04-05 11:18:04 得分 0
Trailing empty strings are therefore not included in the resulting array.
空串的时候不被记录的下来的Top
9 楼hongyan2004(spring+hibernate)回复于 2005-04-05 13:23:52 得分 0
StringTokenizer
可以分割特殊字符Top
10 楼rower203(华仔)回复于 2005-04-05 14:21:44 得分 0
重写split()方法:
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);
就可以了.Top
11 楼rower203(华仔)回复于 2005-04-05 14:26:23 得分 20
// if (limit == 0)
// while (resultSize > 0 && matchList.get(resultSize-1).equals(""))
// resultSize--;
这段程序加上后就是原先那种效果.Top
12 楼adenzhang(佐敦)回复于 2005-04-05 15:49:57 得分 0
花子的方法是正解。这种方法我是知道的,我就是想问如何用java的API来实现。
我写的方法如下:
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]);
}Top




