java 在String Java中获取空格后的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29537004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 15:27:28  来源:igfitidea点击:

To get the value after space in String Java

javastringextract

提问by androidcodes

I have String of values .

我有 String of values 。

Like as

像作为

String line= "cpu  74608   2520   24433   1117073   6176   4054";

i just want to extract these Values and store them in different variables.

我只想提取这些值并将它们存储在不同的变量中。

Is there any way that i can get the Numeric values after every space ??

有什么方法可以让我在每个空格后获得数值?

回答by Dmitry Ginzburg

One of the options you have is to use String.splitmethod. It accepts regex, so you can skip any number of spaces:

您拥有的选项之一是使用String.split方法。它接受正则表达式,因此您可以跳过任意数量的空格:

String[] values = line.split("\s*");

Then you'll have to convert these Strings to the values you need:

然后您必须将这些Strings转换为您需要的值:

int firstNum = Integer.valueOf(values[1]);
// anything like this

If you're OK with storing it as an array/list, but not as different variables, maybe you'll want to extract all numbers this way (Java 8):

如果您可以将其存储为数组/列表,但不能将其存储为不同的变量,那么您可能希望以这种方式提取所有数字(Java 8):

int[] numbers = Stream.of(values)
        .filter(str -> str.matches("-?\d+"))
        .mapToInt(Integer::valueOf)
        .toArray();

Maybe, the easier way would be using Scanner:

也许,更简单的方法是使用Scanner

Scanner scanner = new Scanner(line);
String firstStr = scanner.next();
int firstNum = scanner.nextInt();
// and so on...

回答by mtyurt

    String input = "123 456 123 578";
    String[] numbers = input.split(" ");
    for (String s : numbers) {
        System.out.println(Integer.valueOf(s));
    }

output:

输出:

123
456
123
578

回答by James

You could start off by splitting the data into a array:

您可以首先将数据拆分为数组:

String [] lineSplit = line.split("\s+");

And then begine parsing the data into individual variables:

然后开始将数据解析为单个变量:

int foo = 0;
for(int i = 0; i < lineSplit.length; i++) {
    if(lineSplit[i].matches("\d+") {
        foo = Integer.parseInt(lineSplit[i]);
    }else {
        //Do something else...
    }
}

And repeat the above for different data types and add variable etc. to get the desired result.

并对不同的数据类型重复上述操作并添加变量等以获得所需的结果。

回答by ar4ers

Yes you can. Use String.split()and Integer.parseInt()as show below:

是的你可以。使用String.split()Integer.parseInt()如下图所示:

    String[] split = line.split("\s+");
    List<Integer> numbers = new ArrayList<Integer>();
    for (String num: split) {
        try {
            numbers.add(Integer.parseInt(num));
        } catch (NumberFormatException ex) {
            // not a number
        }
    }

回答by Syed Danish Haider

String thisString="Hello world";

String[] parts = theString.split(" ");
String first = parts[0];//"hello"
String second = parts[1];//"World"