• Post Reply Bookmark Topic Watch Topic
  • New Topic

Correct split() syntax  RSS feed

 
Brandt Charles
Ranch Hand
Posts: 57
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
 
Chad Clites
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Chrome IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant a single space. You all answered my question, thank you very much.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!