• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Correct split() syntax

 
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know very little about regular expressions and related methods. However, I want to use the split method in lieu of StringTokenizer. I want to split a string displayed in a text field by white space. Is this the correct format?

String [] input;
String str;
input[] = str.split(" "); // Is this right to accomplish this?
 
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, when you say "white space", do you mean whitespace? meaning spaces, tabs, etc.? or just a space?

Second, when two spaces are together, is it one delimiter or two?

Assuming that you mean "whitespace", and groups of whitespaces is only one delimiter, then the correct Regular Expression is "\\s+".

Henry
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just for sake of argument (because regular expressions and pattern matching amuse me to no end), is:

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

significantly different than:

input[] = str.split("[ ]");
??
[ July 06, 2006: Message edited by: Chad Clites ]
 
Henry Wong
author
Posts: 23960
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first case will split around a single space, tab, carriage return, form feed, or linefeed. The second case will only split around a single space.

Henry
 
Brandt Charles
Ranch Hand
Posts: 57
IntelliJ IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I meant a single space. You all answered my question, thank you very much.
 
Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic