Config Router

  • Google Sheets
  • CCNA Online training
    • CCNA
  • CISCO Lab Guides
    • CCNA Security Lab Manual With Solutions
    • CCNP Route Lab Manual with Solutions
    • CCNP Switch Lab Manual with Solutions
  • Juniper
  • Linux
  • DevOps Tutorials
  • Python Array
You are here: Home / How to split a String by space

How to split a String by space

October 8, 2021 by James Palmer

What you have should work. If, however, the spaces provided are defaulting to… something else? You can use the whitespace regex:
str = “Hello I’m your String”;
String[] splited = str.split(“\s+”);

This will cause any number of consecutive spaces to split your string into tokens.
As a side note, I’m not sure “splited” is a word 🙂 I believe the state of being the victim of a split is also “split”. It’s one of those tricky grammar things 🙂 Not trying to be picky, just figured I’d pass it on!

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'mu00A0your Stringu00A0"; // 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]

Related

Filed Under: Uncategorized

Recent Posts

  • How do I give user access to Jenkins?
  • What is docker volume command?
  • What is the date format in Unix?
  • What is the difference between ARG and ENV Docker?
  • What is rsync command Linux?
  • How to Add Music to Snapchat 2021 Android? | How to Search, Add, Share Songs on Snapchat Story?
  • How to Enable Snapchat Notifications for Android & iPhone? | Steps to Turn on Snapchat Bitmoji Notification
  • Easy Methods to Fix Snapchat Camera Not Working Black Screen Issue | Reasons & Troubleshooting Tips to Solve Snapchat Camera Problems
  • Detailed Procedure for How to Update Snapchat on iOS 14 for Free
  • What is Snapchat Spotlight Feature? How to Make a Spotlight on Snapchat?
  • Snapchat Hack Tutorial 2021: Can I hack a Snapchat Account without them knowing?

Copyright © 2022 · News Pro Theme on Genesis Framework · WordPress · Log in