简体   繁体   中英

How split string to array (java)

I need split String to array. For exapmle i have string str = "apple fruits money Pacific Ocean". and I try split to array like this:

String []arr = str.split(" ");

But I need the Pacific Ocean to register in one cell of the array. I can't change the separator, because i get data in this form ("apple fruits money Pacific Ocean").

If we admit that multiple consecutive capitalized words need to be considered as a single word, then you can do:

String []arr = str.split("\\\\s");

then `String str = "apple fruits money Pacific Ocean";

    String[] arr = str.split("\\s");

    String[] finalArr = new String[arr.length];
    int i = 0;
    for (String word : arr) {
        // capitalized
        if (Character.isUpperCase(word.charAt(0))) {
            // check if previous is capitalized
            if (Character.isUpperCase(finalArr[i - 1].charAt(0))) {
                finalArr[i - 1] = finalArr[i - 1] + word + " ";
            } else {
                finalArr[i] = word + " ";
            }
        } else {
            finalArr[i] = word;
        }
        i++;
    }
    for (String s : finalArr) {
        System.out.println(s);
    }

}

}`

will result in: apple fruits money Pacific Ocean null You'll need to filter the nulls though and add some checks (if i-1 exists at all).

You need to change the separator as Elliott Frisch stated in his comment. You're not going to be able to determine whether or not a set of words need to stay together if they contain a space. If your word list were separated by another character (such as a comma) then the problem becomes much easier to solve.

String input = "apples,fruits,money,Pacific Ocean";
String[] arr = input.split(",");

Now your array contains each of the words in input .

The problem as described in the question and comments has no solution.

Consider this:

"banana red apple green apple"

This can be split like this:

["banana", "red", "apple", "green", "apple"]

or like this

["banana", "red apple", "green apple"]

Without semantic / contextual analysis it is impossible to know which is more likely to be correct. And it is impossible to know for sure what the (human) user actually meant.


I can't change the separator, because i get data in this form ("apple fruits money Pacific Ocean").

You need to redesign the form or the input syntax so that your software doesn't need to perform this task. There is no other way ... to always get the correct answer.

Think of it this way. Suppose someone gave you a sequence of words in a foreign language on a piece of paper, and asked you to split them correctly. How would you (a human) solve the problem, assuming that you didn't understand the language, and hadn't been given a dictionary or a set of rules? This is equivalent to the task you are setting the computer ...

This way it's not possible. If the string was joined earlier, try using a character other than space. Maybe the pipe | might be an option.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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