How to split a String by space

前端 未结 15 1548
傲寒
傲寒 2020-11-22 10:31

I need to split my String by spaces. For this I tried:

str = \"Hello I\'m your String\";
String[] splited = str.split(\" \");

But it doesn\

相关标签:
15条回答
  • 2020-11-22 10:41

    you can saperate string using the below code

       String thisString="Hello world";
    
       String[] parts = theString.split(" ");
    
       String first = parts[0];//"hello"
    
        String second = parts[1];//"World"
    
    0 讨论(0)
  • 2020-11-22 10:41

    Not only white space, but my solution also solves the invisible characters as well.

    str = "Hello I'm your String";
    String[] splited = str.split("\p{Z}");
    
    0 讨论(0)
  • 2020-11-22 10:43

    OK, so we have to do splitting as you already got the answer I would generalize it.

    If you want to split any string by spaces, delimiter(special chars).

    First, remove the leading space as they create most of the issues.

    str1 = "    Hello I'm your       String    ";
    str2 = "    Are you serious about this question_  boy, aren't you?   ";
    

    First remove the leading space which can be space, tab etc.

    String s = str1.replaceAll("^\\s+","");//starting with whitespace one or more
    

    Now if you want to split by space or any special char.

    String[] sa = s.split("[^\\w]+");//split by any non word char
    

    But as w contains [a-zA-Z_0-9] ,so if you want to split by underscore(_) also use

     String[] sa = s.split("[!,? ._'@]+");//for str2 after removing leading space
    
    0 讨论(0)
  • 2020-11-22 10:50

    I do believe that putting a regular expression in the str.split parentheses should solve the issue. The Java String.split() method is based upon regular expressions so what you need is:

    str = "Hello I'm your String";
    String[] splitStr = str.split("\\s+");
    
    0 讨论(0)
  • 2020-11-22 10:53

    While the accepted answer is good, be aware that you will end up with a leading empty string if your input string starts with a white space. For example, with:

    String str = " Hello I'm your String";
    String[] splitStr = str.split("\\s+");
    

    The result will be:

    splitStr[0] == "";
    splitStr[1] == "Hello";
    splitStr[2] == "I'm";
    splitStr[3] == "Your";
    splitStr[4] == "String";
    

    So you might want to trim your string before splitting it:

    String str = " Hello I'm your String";
    String[] splitStr = str.trim().split("\\s+");
    

    [edit]

    In addition to the trim caveat, you might want to consider the unicode non-breaking space character (U+00A0). This character prints just like a regular space in string, and often lurks in copy-pasted text from rich text editors or web pages. They are not handled by .trim() which tests for characters to remove using c <= ' '; \s will not catch them either.

    Instead, you can use \p{Blank} but you need to enable unicode character support as well which the regular split won't do. For example, this will work: Pattern.compile("\\p{Blank}", UNICODE_CHARACTER_CLASS).split(words) but it won't do the trim part.

    The following demonstrates the problem and provides a solution. It is far from optimal to rely on regex for this, but now that Java has 8bit / 16bit byte representation, an efficient solution for this becomes quite long.

    public class SplitStringTest
    {
        static final Pattern TRIM_UNICODE_PATTERN = Pattern.compile("^\\p{Blank}*(.*)\\p{Blank}$", UNICODE_CHARACTER_CLASS);
        static final Pattern SPLIT_SPACE_UNICODE_PATTERN = Pattern.compile("\\p{Blank}", UNICODE_CHARACTER_CLASS);
    
        public static String[] trimSplitUnicodeBySpace(String str)
        {
            Matcher trimMatcher = TRIM_UNICODE_PATTERN.matcher(str);
            boolean ignore = trimMatcher.matches(); // always true but must be called since it does the actual matching/grouping
            return SPLIT_SPACE_UNICODE_PATTERN.split(trimMatcher.group(1));
        }
    
        @Test
        void test()
        {
            String words = " Hello I'm\u00A0your String\u00A0";
            // non-breaking space here --^ and there -----^
    
            String[] split = words.split(" ");
            String[] trimAndSplit = words.trim().split(" ");
            String[] splitUnicode = SPLIT_SPACE_UNICODE_PATTERN.split(words);
            String[] trimAndSplitUnicode = trimSplitUnicodeBySpace(words);
    
            System.out.println("words: [" + words + "]");
            System.out.println("split: [" + Arrays.stream(split).collect(Collectors.joining("][")) + "]");
            System.out.println("trimAndSplit: [" + Arrays.stream(trimAndSplit).collect(Collectors.joining("][")) + "]");
            System.out.println("splitUnicode: [" + Arrays.stream(splitUnicode).collect(Collectors.joining("][")) + "]");
            System.out.println("trimAndSplitUnicode: [" + Arrays.stream(trimAndSplitUnicode).collect(Collectors.joining("][")) + "]");
        }
    }
    

    Results in:

    words: [ Hello I'm your String ]
    split: [][Hello][I'm your][String ]
    trimAndSplit: [Hello][I'm your][String ]
    splitUnicode: [][Hello][I'm][your][String]
    trimAndSplitUnicode: [Hello][I'm][your][String]
    
    0 讨论(0)
  • 2020-11-22 10:53

    Try this one

        String str = "This is String";
        String[] splited = str.split("\\s+");
    
        String split_one=splited[0];
        String split_second=splited[1];
        String split_three=splited[2];
    
       Log.d("Splited String ", "Splited String" + split_one+split_second+split_three);
    
    0 讨论(0)
提交回复
热议问题