• Post Reply Bookmark Topic Watch Topic
  • New Topic

split method in Java  RSS feed

 
ChuChu Okeke
Greenhorn
Posts: 2
Java Netbeans IDE Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help! Can somebody please explain how the [av] regular expression works in the statements below?

String[] tokens = "Javatavamava".split("[av]");
System.out.println("\n" + tokens[0] + ", " + tokens[1] + ", " + tokens[2] + ", " + tokens[3] );




when I compile, it produces blanks and I'm so confused.
Thanks in advance!

printout is: J, , , t

P.S. where can I get good tutorials on netbeans? Thanks again! : beerchug:
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi and welcome!

Note that the split method uses regex. If you wikipedia it, you'll find that [av] means "a" or "v".

http://en.wikipedia.org/wiki/Regular_expression#POSIX_Basic_Regular_Expressions

If you would like the separator to be "av", you should use

Hope this helps.
Cheers,
Claudiu
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rather than printing 4 tokens, try this:You may be surprised by how many lines you get
 
ChuChu Okeke
Greenhorn
Posts: 2
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ritchie and claudiu. I wanted to know how the compiler treats a or v [av] with the split method.
I think in that case, it takes the first a & v as two separate quantities (explains the two separate spaces or "lines".) so, with more individual letters, the more the spaces or lines...I think.
Thanks, see you again soon...real soon
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!