简体   繁体   English

字符串子字符串有意删除字符0

[英]String Substring intentionally remove character 0

Im currently working on a String parsing function and suddenly i found this problem. 我目前正在处理一个字符串解析函数,突然我发现了这个问题。 For example: 例如:

String data = "1234567890JOHN F DOE";

String ID = data.substring(0,9);
String Name = data.substring(10, 19);

the expected output I want for ID is "1234567890" however the only characters I got is only "123456789" and "0" is removed. 我想要的ID的预期输出是"1234567890"但是我得到的唯一字符只有"123456789"并且删除了"0"

Are there any function I can use instead of substring(...) ? 我可以使用任何功能代替substring(...)吗?

您会收到"123456789"因为substring(...)方法中的end参数不包含在内,因此要获取"1234567890"您需要使用data.substring(0,10) :)

As said in the docs the substring(...) 's ending index is the index inputted minus one. 文档中所述, substring(...)的结束索引是输入的索引减一。 What you want to do is have: 您想要做的是:

String data = "1234567890JOHN F DOE";
String ID = data.substring(0,10); 
String Name = data.substring(10, 20);

Output: 输出:

ID: 1234567890

Name: JOHN F DOE

Using substring you can simply do : str.substring (0,10); 使用子字符串,您可以简单地执行以下操作:str.substring(0,10); This is another way : 这是另一种方式:

 String pattern="\\d+";
    String text="1234567890JOHN F DOE";
    Pattern p=Pattern.compile(pattern);
    Matcher m=p.matcher(text);
    while (m.find()) {
        System.out.println(text.substring(m.start(), m.end()));
    }

Let's understand the startIndex and endIndex you need to do this by the code : 让我们了解您需要通过以下代码执行的startIndex和endIndex:

String data = "1234567890JOHN F DOE";
String ID = data.substring(0,10);
String Name = data.substring(10, 20);
    System.out.println("ID:" + ID);
    System.out.println("Name: "+  Name);

Output: 输出:

ID: 1234567890

Name: JOHN F DOE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM