简体   繁体   English

使用拆分方法拆分字符串

[英]Split String using split method

I have one String text that i would like to split,result i want to get is that when i take text/split output each part like for example: Name: John, Last Name: Davidson, Date of Birth: 05051968, Place of Birth: London. 我有一个要分割的String文本,结果想得到的是,当我进行文本/分割输出时,例如:名称:John,姓:Davidson,生日:05051968,出生地:伦敦。 But i am not getting correct result. 但是我没有得到正确的结果。 my code is following: 我的代码如下:

public class Person{
    public String name;
    public String lastName;
    public String dateOfBirth;
    public String placeOfBirth;

poblic void printDetails(){
    String text = "John.Davidson/0505168/London Micheal.Bartson/06061680/Paris";

    String[] newText = text.split("[./ ]");
    for(int i=0; i<newText.length; i++){
         String name = newText[i].split("")[0];
         String lastName = newText[i].split("")[0];
         String dateOfBirth = newText[i].split("")[0];
         String placeOfBirth = newText[i].split("")[0];
         System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
   }

Result i am getting is following: Name: J, last Name: J, date of birth: J, place of birth: J Name: D, last name: D, date of birth: D, place of birth: D ....... and it goes like that for every first character in text. 我得到的结果如下:姓名:J,姓:J,出生日期:J,出生地点:J名称:D,姓:D,出生日期:D,出生地点:D ... ....就像文本中的每个第一个字符一样。 Please can some one look and tell me where i am mistaking? 请有人看一下,告诉我我在哪里错吗?

The results of the split come in groups of four, so you need to set the step of your loop at 4, and get the individual parts through offsets 0, 1, 2, and 3, like this: 分割的结果以4组为一组,因此您需要将循环的步长设置为4,并通过偏移量0、1、2和3来获得各个部分,如下所示:

for(int i=0; i<newText.length; i+=4){
    String name = newText[i];
    String lastName = newText[i+1];
    String dateOfBirth = newText[i+2];
    String placeOfBirth = newText[i+3];
    System.out.println("Name: " + name + ", last name: " + lastName + ", date of birth: " + dateOfBirth + ", place of birth: " + placeOfBirth);
}

Demo. 演示

You're splitting using the "" which means split every character. 您使用“”进行拆分,这意味着拆分每个字符。 Then you take the first character. 然后,您选择第一个字符。 I don't know why you're doing it that way. 我不知道你为什么要那样做。

In summary, what happens in every loop is it takes the first character ([0]) of element i of the array, then sets every single value that wil lbe printed in the string to that character. 总之,在每个循环中发生的事情是,它获取数组元素i的第一个字符([0]),然后将要打印在字符串中的每个单个值都设置为该字符。 Instead, try this 而是试试这个

String[] newText = text.split("[./ ]");
for(int i = 0; i < newText.length - 4; i+=4){
    System.out.println("Name: " + newText[i] + ", last name: " + newText[i+1] + ", date of birth: " + newText[i+2] + ", place of birth: " + newText[i+3]);
}

However, this is a terrible solution, it relies on fixed sized entries and should not be used in practice. 但是,这是一个糟糕的解决方案,它依赖于固定大小的条目,因此不应在实践中使用。 What if someone enters the string in a different order, or with one too many inputs or one too few? 如果有人以不同顺序输入字符串,或者输入太多或输入太少怎么办? Try using more flexible designs, such as the usage of a csv format parser, so you always split using commas, and the rows can be something like 尝试使用更灵活的设计,例如使用csv格式解析器,因此您始终使用逗号进行拆分,并且行可能类似于

entry-type, entry
entry-type, entry2
entry-type, entry3

Or something like that. 或类似的东西。 Try it out. 试试看。 Always try to aim for flexible solutions that don't rely on exact input to work, otherwise you will have exceptions and runtime issues like there's no tomorrow. 始终尝试寻求不依赖确切输入来工作的灵活解决方案,否则您将遇到异常情况和运行时问题,例如没有明天。

PS the point of the split() method is to split the string between occurences of the specified input, ie [./], so don't use it if you want to just give a "", that's no different than making a charArray (except instead of char[] it is String[]) PS split()方法的要点是在出现的指定输入(即[./])之间分割字符串,因此,如果您只想给出“”,请不要使用它,这与制作charArray没什么不同(除了不是char []而是String [])

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

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