专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »Java教程 » split分割:split分割字符串,空结果不能得到的问题 »正文

split分割:split分割字符串,空结果不能得到的问题

来源: 发布时间:星期三, 2008年9月10日 浏览:272次 评论:0
先看源代码

view plaincopy to clipboardprint?
/**
*
* @author 赵学庆 <A href="http://www.java2000.net">www.java2000.net</A>
*
*/
class T {
public static void main(String args[]) {
String num[] = new String[11];
String sLine = "101494|360103660318444|2008/06/17|周润英|1292.0|3085.76|2778.28|912.91|106.0|||";
num = sLine.split("\\|");
int row = 1;
for (String s : num) {
System.out.println(row+++"="+s);
}
}
}

/**
*
* @author 赵学庆 www.java2000.net
*
*/
class T {
public static void main(String args[]) {
String num[] = new String[11];
String sLine = "101494|360103660318444|2008/06/17|周润英|1292.0|3085.76|2778.28|912.91|106.0|||";
num = sLine.split("\\|");
int row = 1;
for (String s : num) {
System.out.println(row+++"="+s);
}
}
}运行结果为

1=101494

2=360103660318444

3=2008/06/17

4=周润英

5=1292.0

6=3085.76

7=2778.28

8=912.91

9=106.0

查看API,有一个

view plaincopy to clipboardprint?
public String[] split(String regex, int limit);

public String[] split(String regex, int limit);limit 参数控制应用模式的次数,从而影响结果数组的长度。

如果限制 n 大于零,那么模式至多应用 n> - 1 次,数组的长度不大于 n,并且数组的最后条目将包含除最后的匹配定界符之外的所有输入。

如果 n 非正,那么将应用模式的次数不受限制,并且数组可以为任意长度。

如果 n 为零,那么应用模式的次数不受限制,数组可以为任意长度,并且将丢弃尾部空字符串。 修改代码为



view plaincopy to clipboardprint?
/**
*
* @author 赵学庆 <A href="http://www.java2000.net">www.java2000.net</A>
*
*/
class T {
public static void main(String args[]) {
String num[] = new String[11];
String sLine = "101494|360103660318444|2008/06/17|周润英|1292.0|3085.76|2778.28|912.91|106.0|||";
num = sLine.split("\\|",-1); // 这里使用-1作为参数
int row = 1;
for (String s : num) {
System.out.println(row+++"="+s);
}
}
}

/**
*
* @author 赵学庆 www.java2000.net
*
*/
class T {
public static void main(String args[]) {
String num[] = new String[11];
String sLine = "101494|360103660318444|2008/06/17|周润英|1292.0|3085.76|2778.28|912.91|106.0|||";
num = sLine.split("\\|",-1); // 这里使用-1作为参数
int row = 1;
for (String s : num) {
System.out.println(row+++"="+s);
}
}
}运行结果

1=101494

2=360103660318444

3=2008/06/17

4=周润英

5=1292.0

6=3085.76

7=2778.28

8=912.91

9=106.0

10=

11=

12=

结果正常
0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: