尝试拆分长字符串并将其数据用于列表

时间:2016-01-08 21:55:05

标签: java string

我有这个

 String t=  "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego"

我创建了一个新的java类Users,其中包含一些空字段,如first_name,last_name等。 我正在尝试使用字符串t中的数据来填充类Users中的那些空字段,因为在那之后,这些字段应该在列表中实例化。

为了更清楚,我正在尝试使输出看起来像这样:

 Name: Mark
 Surname: Labinson
 Birthday: 11 may 1985
 Birthplace : Chicago

6 个答案:

答案 0 :(得分:2)

你也可以这样做:

  

UsersTest类

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

public class UsersTest {

    public static void main(String[] args) throws ParseException {
        String record =  "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego";
        String[] users = record.split("\\s+");

        List<User> userList = new ArrayList<User>();
        for(String user : users) {
            userList.add(new User(user.split("\\/|\\.")));
        }
        System.out.println("userList : "+ userList);
    }
}
  

用户类

import java.text.ParseException;
import java.text.SimpleDateFormat;

public class User {

    private SimpleDateFormat inputFormat = new SimpleDateFormat("ddMMyyyy");
    private SimpleDateFormat outputFormat = new SimpleDateFormat("dd MMM yyyy");
    private String firstName;
    private String lastName;
    private String birthDate;
    private String birthPlace;

    public User(String[] user) throws ParseException {
        this(user[0], user[1], user[2], user[3]);
    }

    public User(String firstName, String lastName, String birthDate, String birthPlace) throws ParseException {
        this.firstName = firstName;
        this.lastName = lastName;
        this.birthDate = outputFormat.format(inputFormat.parse(birthDate));
        this.birthPlace = birthPlace;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getBirthDate() {
        return birthDate;
    }

    public String getBirthPlace() {
        return birthPlace;
    }

    @Override
    public String toString() {
        return "User [firstName=" + firstName + ", lastName=" + lastName
                + ", birthDate=" + birthDate + ", birthPlace=" + birthPlace
                + "]\n";
    }
}
  

输出:

userList : [User [firstName=Mark, lastName=Labinson, birthDate=11 May 1985, birthPlace=Chicago]
, User [firstName=Michael, lastName=Louis, birthDate=25 Aug 1972, birthPlace=NewYork]
, User [firstName=Gabriel, lastName=Vitton, birthDate=05 May 1988, birthPlace=SanDiego]
]

答案 1 :(得分:1)

尝试使用此功能将字符串分隔为/space.

String t = "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego";
List<String> items = Arrays.asList(t.split("(/|\\.|\\s)+"));
SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy");
for (int i = 0; i < items.size(); i += 4) {
    System.out.println("Name: " + items.get(i));
    System.out.println("Surname: " + items.get(i + 1));
    try {
        System.out.println("Birthday: " + format.parse(items.get(i + 2)));
    } catch (ParseException e) {
        e.printStackTrace();
    }  
    System.out.println("Birthplace: " + items.get(i + 3));
    System.out.println();
}

输出:

  

姓名:马克
  姓:Labinson
  生日:5月11日星期六00:00:00 CEST 1985年   出生地:芝加哥

     

姓名:迈克尔   姓:路易郎   生日:8月25日星期五00:00:00 CET 1972年   出生地:纽约

     

姓名:加布里埃尔   姓:Vitton
  生日:2005年5月5日00:00:00 CEST 1988年   出生地:SanDiego

答案 2 :(得分:1)

尝试拆分。

String t=  "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego";

   String[] tArray = t.split("\\/|\\.");

        for (int i = 0; i < tArray.length; i++) {
            System.out.println(tArray[i]);
        }

答案 3 :(得分:1)

使用以下代码

public static void main(String[] s) throws ParseException {
    String t=  "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego";

    String[] items = t.split("/");
    String[] nameSurname = items[0].split("\\.");
    String[] places = items[2].split(" ");
    SimpleDateFormat format = new SimpleDateFormat("ddMMyyyy");
    System.out.println("Name: " + nameSurname[0]); // Name: Mark
    System.out.println("Surname: " + nameSurname[1]); // Surname: Labinson
    System.out.println("Birthday: " + format.parse(items[1])); // Birthday: Sat May 11 00:00:00 CEST 1985
    System.out.println("Birthplace: " + places[0]); // Birthplace: Chicago
}

答案 4 :(得分:1)

您可以通过多种方式完成此操作。您可以使用正则表达式来拆分字符串或仅作为String类的一部分的基本函数。这是一种仅使用字符串类和日期格式化程序的方法。

public static void main(String[] args) throws ParseException {
    String t =  "Mark.Labinson/11051985/Chicago Michael.Louis/25081972/NewYork Gabriel.Vitton/05051988/SanDiego";

    String[] userInputs = t.split(" ");

    for(String input : userInputs){
        SimpleDateFormat dateInputFormat = new SimpleDateFormat("ddMMyyyy");
        SimpleDateFormat dateOutputFormat = new SimpleDateFormat("dd MMM yyyy");

        String[] fields = input.split("/");
        String firstName = fields[0].substring(0, fields[0].indexOf("."));
        String lastName = fields[0].substring(fields[0].indexOf(".")+1);
        Date birthdate = dateInputFormat.parse(fields[1]);
        String birthplace = fields[2];          

        System.out.println("Name:\t\t" + firstName);
        System.out.println("Surname:\t" + lastName);
        System.out.println("Birthday:\t" + dateOutputFormat.format(birthdate));
        System.out.println("Birthplace:\t" + birthplace + "\n");
    }
}

答案 5 :(得分:0)

如果你想使用一个哈希集:

/**
* Read a line of text from standard input (the text
* terminal), and return it as a set of words.
*
* @return A set of Strings, where each String is one of the
* words typed by the user
*/
public HashSet<String> getInput()
{
   System.out.print("> "); // print prompt
   String inputLine = reader.nextLine().trim().toLowerCase();
   String[] wordArray = inputLine.split("(\\/|\\.|\\s)+"));
   // add words from array into hashset
   HashSet<String> words = new HashSet<String>();
   for(String word : wordArray) {
      words.add(word);
   }
   return words;
}