简体   繁体   中英

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. 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. 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:

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. 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

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[])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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