关于substring的问题

codeMoe 2010-10-09 01:20:49
近日在做一个连接网络的软件,在接受服务器返回的数据,读取中,出了个问题:

//第一类别参数的第三参数是记录当前目录数量
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
System.out.println("temp1_3:"+temp1);
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")));
classListNum = Integer.parseInt(temp1.substring(0,temp.trim().indexOf(",")));//因为temp1已经从第一个“,”号开始,所以开始索引为0

这段我输出在控制台的数据是:
temp1_3:10,
temp1_4:1

正确的话应该是
temp1_3:10,
temp1_4:10
才对

想问问各位大虾,这个问题出在哪里?
分数也就只有这么多,求各位大大解救,谢谢
...全文
79 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
哦~~用shanxmxj大大的建议temp1.substring(0, temp1.indexOf(","))
真的解决了,测试多次,目前没有问题,感谢shanxmxj大大
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
String temp1 = temp.trim();//临时保存temp的变量
//第一类别参数的第一参数是当前页数
pageCount = temp1.substring(0, temp.trim().indexOf(","));

//第一类别参数的第二参数是记录当前类别页数总数
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
System.out.println("temp1_1:"+temp1.substring(0, temp.trim().indexOf(",")));
System.out.println("temp1_2:"+temp1);
pageNUM = Integer.parseInt(temp1.substring(0, temp.trim().indexOf(",")));//因为temp1已经从第一个“,”号开始,所以开始索引为0

//第一类别参数的第三参数是记录当前目录数量
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
System.out.println("temp1_3:"+temp1);
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")));
classListNum = Integer.parseInt(temp1.substring(0,temp.trim().indexOf(",")));//因为temp1已经从第一个“,”号开始,所以开始索引为0

输出是
temp1_1:2
temp1_2:2,10,
temp1_3:10,
temp1_4:1
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
这里temp1的值就是10,
shanxmxj 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 codemoe 的回复:]
不是,因为开始完整的数据temp是
2,10,
经过那temp1的分开现在temp是
10,

而我要拿 10 出来,不知为什么只能拿到 1
[/Quote]

在哪里分开了??
shanxmxj 2010-10-09
  • 打赏
  • 举报
回复
temp1.substring(0, temp1.indexOf(","))
那怎么不直接用temp1取呢??
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
现在就是想问
temp1.substring(0, temp.trim().indexOf(","))
这里,temp已经是 10,
按理说 temp1是10才对,
为什么只有拿到 1 ?
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
但我
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")+1));
的话,就能完成拿出 10出来。问题何在?这里不能temp.trim().indexOf(",")+1));不然有些数据是个位数的就出错了
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
不是,因为开始完整的数据temp是
2,10,
经过那temp1的分开现在temp是
10,

而我要拿 10 出来,不知为什么只能拿到 1
shanxmxj 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 codemoe 的回复:]
temp1也是string变量
[/Quote]
没问是什么类型

我的意思是
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")));
你是不是写错了??? 应该是temp1吧
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
String temp1 = temp.trim();//临时保存temp的变量

temp是接受网络数据
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
temp1也是string变量
codeMoe 2010-10-09
  • 打赏
  • 举报
回复
temp是string变量
shanxmxj 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 codemoe 的回复:]
近日在做一个连接网络的软件,在接受服务器返回的数据,读取中,出了个问题:

//第一类别参数的第三参数是记录当前目录数量
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
System.out.println("temp1_3:"+temp1);
System.out.println("temp1_4……
[/Quote]
上面是temp1
下面的是temp
temp值是多少
temp1值又是多少?
dr_lou 2010-10-09
  • 打赏
  • 举报
回复
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")));
dr_lou 2010-10-09
  • 打赏
  • 举报
回复
temp1 = temp1.substring(temp1.indexOf(",")+1);//修改temp1所保存的值,方便下面调用
System.out.println("temp1_3:"+temp1);
System.out.println("temp1_4:"+temp1.substring(0, temp.trim().indexOf(",")));


temp是谁?

13,100

社区成员

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

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