无法分割字符串

huangapple go评论77阅读模式
英文:

Unable to split string

问题

我有一个字符串 002999002020,我想对其进行分割,得到结果 992020。我尝试了以下代码:

String[] s1 = selectedMeterNo.split(String.valueOf(4),2);
String[] s2 = selectedMeterNo.split(String.valueOf(9),3);

System.out.println(Arrays.toString(s1));System.out.println(Arrays.toString(s2)); 的输出结果为空。

英文:

I have a string 002999002020 which I want to split and get a result 992020. I have tried following

String[] s1 = selectedMeterNo.split(String.valueOf(4),2);
String[] s2 = selectedMeterNo.split(String.valueOf(9),3);

The output of s1 and s2 is empty.

答案1

得分: 1

你可以尝试使用子字符串。

String s1 = selectedMeterNo.substring(4, 6);
String s2 = selectedMeterNo.substring(9, 12);
String s3 = s1 + s2;
英文:

You can try substring.

String s1 = selectedMeterNo.substring(4,6);
String s2 = selectedMeterNo.substring(9,12);
String s3 = s1+s2;

huangapple
  • 本文由 发表于 2020年8月15日 15:24:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63423623.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定