File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problem in string's split method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in string Watch "problem in string New topic

problem in string's split method

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
String st="";
String [] st1=st.split(".");
System.out.println("string split ="+st1.length );

above code gives me empty array of String how to change my regex expression i.e "."

so that this method will give give me array with 4 elements=172,12,23,23
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Try putting \\ in front of . : "\\."

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
Rudy Rusli
Ranch Hand

Joined: Jun 01, 2006
Posts: 114
This would also work:
String [] st1=st.split("[.]");
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3749

You might be thinking of StringTokenizer which can split a string into tokens based on a single value like a "."

[OCA 8 Book] [Blog]
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Stick with String.split(), it works great and besides StringTokenizer is a legacy class kept around to not break old code, but shouldn't be used on new code.
I agree. Here's the link:
subject: problem in string's split method
It's not a secret anymore!