• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Split string and keep all spaces.

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have a problem to solve. I have  a string: "hello world   !!!" for example. And on output a need to have an array like this
["hello", " ", "world", "   ", "!!!"]

I was trying to do it with regex, but possibly i use a wrong one.

This my code

public static String test(String s){        
   String[] words = s.split("\\b");  
   return Arrays.toString(words);  
 }


It give me an array with spaces , but I dont understand why, but words[0] contains empt element:
[, This,  , is,  , test]

 
Ranch Hand
Posts: 125
1
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"\b" just matches the end of a word character, which is space. But, since you specified only one character match, <space><space>, is interpreted as <space><empty word><space>, and that is why you are getting an empty word as one of the values. Try "\b+" or "\s+" so that it matches all the spaces between the words.
 
Marshal
Posts: 65113
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure what the correct answer is, but using \s will produce the resultand \s+ will produce this result:-Your spaces will be regarded as part of the delimiter, but OP needs the intervening spaces retained as part of the text. The problem is that ! doesn't count as a word character for regexes, so the \b regex will ignore it. That is why you are getting “   !!!” as the last element of the array; \b doesn't distinguish the spaces from the exclamation marks.
I am by no means an expert on regexes, but is it possible to create a straightforward regex to distinguish spaces from ! at all?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!