数组分割字符串

pingsuccess 2011-04-27 10:07:08
将例如:String temp=",2011,,,";

将它分割成String[] str={"","2011","",""};

谢谢各位了,帮帮忙啦!
...全文
191 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqh3672 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lin186 的回复:]
Java code

String no = "123,456,789,,,,";
no = no.replace(",,", ", ,");//将两个逗号之间加个空格
String[] nos = no.split(",");
for(int i=0;i<nos.length;i++){
System.out.p……
[/Quote]
支持简洁
ForUForM 2011-04-27
  • 打赏
  • 举报
回复

String no = "123,456,789,,,,";
no = no.replace(",,", ", ,");//将两个逗号之间加个空格
String[] nos = no.split(",");
for(int i=0;i<nos.length;i++){
System.out.println("nos["+i+"]:"+nos[i]);
}

输出结果:
nos[0]:123
nos[1]:456
nos[2]:789
nos[3]:
nos[4]:
nos[5]:
steryzone 2011-04-27
  • 打赏
  • 举报
回复

String str = ",2011,,,";
str = str.replaceAll(",",", ");
String[] str1 = str.split("\\,");
lqh3672 2011-04-27
  • 打赏
  • 举报
回复

import java.util.ArrayList;
import java.util.List;

public class Test {
private List templist = new ArrayList();
private String lastS = "";

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

String temp = ",2011,,,";
Test test = new Test();
test.getSplitString(temp);
String[] rs = new String[test.templist.size()];
for (int i = 0; i < test.templist.size(); i++) {
rs[i] = test.templist.get(i).toString();
System.out.println(i + "---" + test.templist.get(i).toString());
}
}

private void getSplitString(String x) {
String temp = "";
if (x.contains(",")) {
temp = x.substring(0, x.indexOf(","));
lastS = x.substring(x.indexOf(",") + 1);
templist.add(temp);
if (lastS.contains(",")) {
getSplitString(lastS);
}
}
}
}
乱写,所以不精简,结果在String[] rs
steryzone 2011-04-27
  • 打赏
  • 举报
回复

String str = ",2011,,,";
Pattern p = Pattern.compile(".*?\\,");
Matcher m = p.matcher(str);
while(m.find()){
System.out.println(m.group());
}
pingsuccess 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lin186 的回复:]
将例如:String temp=",2011,,,";
将它分割成String[] str={"","2011","",""};

String temp=",2011,,,";
String[] tempArrs = temp.split(",");//按字符串中的逗号分割开来;
然后tempArrs 就好比是你上面的str={"","2011","",""};了。
[/Quote]

这种不行啦,我试过了,因为temp字符串后面的是值是空的,如果以这种方式的话,就成了

tempArrs=["","2011"];这种格式了,好像把后面的值就没了
pingsuccess 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wangjiangbo2 的回复:]
temp.replaceAll(",",",#")
temp.split("#")
没测试不知道可以不
[/Quote]

不行,你这种就把它变成这种了.String[] str=[",2011,,,"];
把它变成了一个值
ForUForM 2011-04-27
  • 打赏
  • 举报
回复
将例如:String temp=",2011,,,";
将它分割成String[] str={"","2011","",""};

String temp=",2011,,,";
String[] tempArrs = temp.split(",");//按字符串中的逗号分割开来;
然后tempArrs 就好比是你上面的str={"","2011","",""};了。

wangjiangbo2 2011-04-27
  • 打赏
  • 举报
回复
temp.replaceAll(",",",#")
temp.split("#")
没测试不知道可以不
JavaMan_KA 2011-04-27
  • 打赏
  • 举报
回复
public class Test {
public static void main(String[] args) {
String string="a,b,c,d,e ,f,g";
String Count[] = string.split(","); //遇到空格就拆分。
for(int i=0;i<Count.length;i++)
{
System.out.println(Count[i]); //输出数组Count的数
}
}
}
休谱诺斯 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lin186 的回复:]

Java code

String no = "123,456,789,,,,";
no = no.replace(",,", ", ,");//将两个逗号之间加个空格
String[] nos = no.split(",");
for(int i=0;i<nos.length;i++){
System.out.print……
[/Quote]这个比较好
dangerous_ 2011-04-27
  • 打赏
  • 举报
回复
刚刚测试的 没有问题
dangerous_ 2011-04-27
  • 打赏
  • 举报
回复

public static void main(String[] args)
{
String str = ",,123,,";
String substr = str;
int count = 0;
while (substr.contains(","))
{
substr = substr.substring(substr.indexOf(",")+1);
count ++;
}
String[] arr = str.split(",", count + 1);
for (int i = 0; i < arr.length; i++)
{
System.out.println(String.format("%s %s", i, arr[i]));
}
}
ETCentury 2011-04-27
  • 打赏
  • 举报
回复

public class StringSplit {

public static void main(String args[]){
String temp=",2011,,,";
String[] str = temp.split(",", -1);
for(String c : str){
System.out.println("标示开始" + c + "标示结束");
}
}
}

这个就可以了
steryzone 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 pingsuccess 的回复:]
引用 7 楼 steryzone 的回复:
Java code

String str = ",2011,,,";
str = str.replaceAll(",",", ");
String[] str1 = str.split("\\,");


这种的是不可以的,这样的话,显示出来的值中包含空格
[/Quote]
那用正式表达式呢
Mr傅 2011-04-27
  • 打赏
  • 举报
回复
覆盖String类的split方法吧,呵呵。
pingsuccess 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 lin186 的回复:]
Java code

String no = "123,456,789,,,,";
no = no.replace(",,", ", ,");//将两个逗号之间加个空格
String[] nos = no.split(",");
for(int i=0;i<nos.length;i++){
System.out.p……
[/Quote]

这样显示出来的值中包含空格,是不行的
pingsuccess 2011-04-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 steryzone 的回复:]
Java code

String str = ",2011,,,";
str = str.replaceAll(",",", ");
String[] str1 = str.split("\\,");
[/Quote]

这种的是不可以的,这样的话,显示出来的值中包含空格

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧