将字符串(Last \ tTFirst \ tAge)转换为包含元素{Last,First,Age}的数组

时间:2022-06-06 21:19:45

I'm new to Java and I want to convert a string such as Apple\tJohn\t23(note \t are tabs) into an array with 3 elements {Apple, John, 23}. I know that I need to some import some string manipulation method but I'm only beginning Java so is there one that fits my situation?

我是Java新手,我想将一个字符串如Apple \ tJohn \ t23(注意\ t是标签)转换成一个包含3个元素{Apple,John,23}的数组。我知道我需要一些导入一些字符串操作方法,但我只是开始Java所以有一个适合我的情况?

2 个解决方案

#1


3  

It sounds like you want:

这听起来像你想要的:

String[] bits = text.split("\t");

Note that split takes a regular expression - so if you ever want to split on a dot, you'd need split("\\.") for example. Personally I prefer the Guava library Splitter class instead, but it's your choice :)

请注意,split采用正则表达式 - 因此,如果您想要分割点,则需要拆分(“\\。”)。我个人更喜欢Guava库Splitter类,但它是你的选择:)

EDIT: From a comment:

编辑:从评论:

Also do you recommend I use an String array to store a mix of integer and character values? Say for futureproofing I might want to do a comparison on say if (age > 21) then John can drink.

您还建议我使用String数组来存储整数和字符值的混合吗?说对于未来的防护,我可能想比较一下,如果(年龄> 21岁)那么约翰可以喝酒。

No. Once you've split the data into a string array, it would be good to convert that into an object specifically for this data. For example:

不会。一旦将数据拆分为字符串数组,最好将其转换为专门用于此数据的对象。例如:

String[] bits = text.split("\t");
// Assuming a suitable constructor...
Person person = new Person(bits[1], bits[0], Integer.parseInt(bits[2]));
System.out.println(person.getFavouriteFruit()); // Apple

#2


1  

Use String.split()

String str = "Apple\tJohn\t23";
    String[] parts = str.split("\t");
    System.out.println(Arrays.toString(parts));

The string "boo:and:foo", for example, yields the following results with these expressions:

例如,字符串“boo:and:foo”会产生以下结果:

Regex   Result
:   { "boo", "and", "foo" }
o   { "b", "", ":and:f" }

#1


3  

It sounds like you want:

这听起来像你想要的:

String[] bits = text.split("\t");

Note that split takes a regular expression - so if you ever want to split on a dot, you'd need split("\\.") for example. Personally I prefer the Guava library Splitter class instead, but it's your choice :)

请注意,split采用正则表达式 - 因此,如果您想要分割点,则需要拆分(“\\。”)。我个人更喜欢Guava库Splitter类,但它是你的选择:)

EDIT: From a comment:

编辑:从评论:

Also do you recommend I use an String array to store a mix of integer and character values? Say for futureproofing I might want to do a comparison on say if (age > 21) then John can drink.

您还建议我使用String数组来存储整数和字符值的混合吗?说对于未来的防护,我可能想比较一下,如果(年龄> 21岁)那么约翰可以喝酒。

No. Once you've split the data into a string array, it would be good to convert that into an object specifically for this data. For example:

不会。一旦将数据拆分为字符串数组,最好将其转换为专门用于此数据的对象。例如:

String[] bits = text.split("\t");
// Assuming a suitable constructor...
Person person = new Person(bits[1], bits[0], Integer.parseInt(bits[2]));
System.out.println(person.getFavouriteFruit()); // Apple

#2


1  

Use String.split()

String str = "Apple\tJohn\t23";
    String[] parts = str.split("\t");
    System.out.println(Arrays.toString(parts));

The string "boo:and:foo", for example, yields the following results with these expressions:

例如,字符串“boo:and:foo”会产生以下结果:

Regex   Result
:   { "boo", "and", "foo" }
o   { "b", "", ":and:f" }