wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes split method in string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "split method in string" Watch "split method in string" New topic
Author

split method in string

Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Hi,

I have a simple string and want to split the string based on a regex. I am using split method to split the string
For Example:


Here the array size is zero.

If i replace with str="fname,lname"; and use str.split(",");

It works fine. Is there anything wrong with the period.

Thanks,
Srikanth.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18486
    
  40

It works fine. Is there anything wrong with the period.


A period has special meaning in a regex. It means to match anything. So, if you want to match an actual period, you will need to escape it... use "\\." instead.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Saathvik Reddy
Ranch Hand

Joined: Jun 03, 2005
Posts: 228
Huh! it worked.
Thanks Henry.
Piet Verdriet
Ranch Hand

Joined: Feb 25, 2006
Posts: 266
Originally posted by Henry Wong:


A period has special meaning in a regex. It means to match anything...


Except new line characters (by default).
 
Consider Paul's rocket mass heater.
 
subject: split method in string
 
Similar Threads
Splitting the String to get all characters
Doubt in split().
Split... multiple spaces
String.split question
Correct split() syntax