简体   繁体   English

用空格和换行符分割字符串

[英]splitting string by spaces and new lines

I'm trying to split a string into an array.我正在尝试将字符串拆分为数组。 This should be fine as str.split(" ") should work fine, however the string is actually in the form of "xyz 100b\nabc 200b\ndef 400b" .这应该没问题,因为str.split(" ")应该可以正常工作,但是字符串实际上是"xyz 100b\nabc 200b\ndef 400b"的形式。 I'm wondering what the best way to handle this is.我想知道处理这个问题的最佳方法是什么。 I also need to return 4 strings in the format they gave.我还需要以他们提供的格式返回 4 个字符串。 Below is how I'm trying it now, but it's not splitting the array up correctly.下面是我现在尝试的方法,但它没有正确拆分数组。 My goal is to get the array split to ["xyz", "100b", "abc", "200b", "def", "400b"]我的目标是将数组拆分为["xyz", "100b", "abc", "200b", "def", "400b"]

public static String solution(String words){
    String array[] = words.split(" ");

    /*
    There's a lot of other code manipulating the array to get 4 figures in the
    end. This doesn't matter, it's just splitting the array and the return that
    is my issue
    In the end I will have 4 integers that I want to return in a similar way
    that they gave them to me.
    */

    return "answer 1" + String.valueOf(num1) + "b\n" + 
           "answer2 " + String.valueOf(num2) + "b\n" +
           "answer 3" + String.valueOf(num3) + "b\n" + 
           "answer4 " + String.valueOf(num4) + "b\n";

}

EDIT:编辑:

String array [] = str.split("\n| ") will split the array as I needed, thank you A.Oubidar String array [] = str.split("\n| ")将根据需要拆分数组,谢谢 A.Oubidar

I hope i understood your question right but if you're looking to extract the number and return them in the specific format you could go like this:我希望我理解你的问题,但如果你想提取数字并以特定格式返回它们,你可以像这样 go :

    // Assuming the String would be like a repetition of [word][space][number][b][\n]
    String testString = "xyz 100b\nabc 200b\ndef 400b";

    // Split by both endOfLine and space
    String[] pieces = testString.split("\n| ");

    String answer = "";

    // pair index should contain the word, impair is the integer and 'b' letter
    for (int i = 0; i < pieces.length; i++) {
        if(i % 2 != 0 ) {
            answer = answer + "answer " + ((i/2)+1) + ": " + pieces[i] + "\n";
        }
    }
    System.out.println(answer);

This is the value of answer after execution:这是执行后 answer 的值:

answer 1: 100b
answer 2: 200b
answer 3: 400b

You should put this code in the "return" instead of the one you already have:您应该将此代码放在“return”中,而不是您已经拥有的代码中:

    return "answer 1" + array[0] + "b\n" + 
       "answer 2 " + array[1] + "b\n" +
       "answer 3" + array[2] + "b\n" + 
       "answer 4 " + array[3] + "b\n";
  1. The split() method takes one regular expression as argument. split()方法将一个正则表达式作为参数。 This: input.split("\\s+") will split on whitespace (\s = space, + = 1 or more).这: input.split("\\s+")将在空白处拆分(\s = 空格,+ = 1 或更多)。

  2. Your question is unclear, but if you're supposed to extract the '100', '200', etc, regular expressions are also pretty good at that.你的问题不清楚,但如果你应该提取“100”、“200”等,正则表达式也很擅长。 You can throw each line through a regular expression to extract the value.您可以通过正则表达式抛出每一行以提取值。 There are many tutorials (just google for 'java regexp example').有很多教程(只是谷歌'java regexp example')。

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

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