简体   繁体   English

在Java中拆分字符串

[英]Splitting up a string in Java

Can anyone advise on an issue that's been bugging me? 任何人都可以就一个困扰我的问题提出建议吗? I have a Java .jar file which returns a string from a method made up as follows: 我有一个Java .jar文件,它从一个方法返回一个字符串,如下所示:

integer integer integer block_of_text

That is three integers which may be positive or negative, each separated by single spaces, and then another space before a block of text which may consist of any characters but which should not contain carriage returns. 这是三个整数,可以是正数或负数,每个整数用单个空格分隔,然后是一个文本块之前的另一个空格,它可以包含任何字符,但不应包含回车符。 Now I suppose I can read a substring up to the space character for each of the initial integers and then simply read the remaining text in one go. 现在我想我可以读取每个初始整数的空格字符的子字符串,然后只需一次读取剩余的文本。

I should add that the block of text is not to be broken down regardless of what it contains. 我应该补充一点,无论文本块包含什么内容,都不会被分解。

But can anyone suggest a better alternative? 但有人可以提出更好的选择吗?

Thanks to the respondents. 感谢受访者。 This has saved me a headache! 这让我头疼!

You can use the form of String#split(regex,limit) which takes a limit : 你可以使用String#split(regex,limit)的形式, 它有一个限制

String s = "123 456 789 The rest of the string";
String ss[] = s.split(" ", 4);
// ss = {"123", "456", "789", "The rest of the string"};

You can use String.split with space as a separator, and set a limit of 4: 您可以将String.split与space用作分隔符,并将限制设置为4:

String[] result = s.split(" ", 4);

You could also use a Scanner . 您也可以使用Scanner This will not only split the text, but also parse the integers to int . 这不仅会拆分文本,还会将整数解析为int Depending on whether you need the three integers as int or String , you might find this more convenient: 根据您是否需要将三个整数作为intString ,您可能会发现这更方便:

Scanner scanner = new Scanner(s);
int value1 = scanner.nextInt();
int value2 = scanner.nextInt();
int value3 = scanner.nextInt();
scanner.skip(" ");
String text = scanner.nextLine();

See it working online: ideone . 看到它在线工作: ideone

One simple way (since you said there won't be newlines in block_of_text) is to use a Scanner . 一种简单的方法(因为你说在block_of_text中没有新行)是使用扫描仪 It is a tool to break up input based upon a certain delimited, and return appropriate types back to you. 它是一种基于特定分隔符分解输入的工具,并将适当的类型返回给您。 For example, you can use the methods hasNextInt() and nextInt() to check for an integer in the input, and to actually pull it from the stream. 例如,您可以使用方法hasNextInt()nextInt()来检查输入中的整数,并实际从流中提取它。

For example: 例如:

Scanner scanner = new Scanner(System.in); // Arg could be a String, another InputStream, etc
int[] values = new int[3];
values[0] = scanner.nextInt();
values[1] = scanner.nextInt();
...
String description = scanner.nextLine();

You could use this until you've exhausted the input stream, and begin using the values as you need. 您可以使用此功能,直到您已用尽输入流,并根据需要开始使用这些值。

Here are more details on how to use Scanner : If ... is not an int { 以下是有关如何使用Scanner更多详细信息: 如果...不是int {

The most simple way to split the string is by using the split method. 拆分字符串的最简单方法是使用split方法。

String gen = "some texts here";
String spt[] = gen.split(" ");  //This will split the string gen around the spaces and stores them into the given string array


To remove the integers according to your question, start the array index from 3. 要根据您的问题删除整数,请从3​​开始数组索引。
I hope this would help. 我希望这会有所帮助。

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

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