Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in string's split method

 
ganesh pol
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String st="172.12.23.23";
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
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try putting \\ in front of . : "\\."
 
Rudy Rusli
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This would also work:
String [] st1=st.split("[.]");
 
Scott Selikoff
author
Saloon Keeper
Posts: 4015
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be thinking of StringTokenizer which can split a string into tokens based on a single value like a "."
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic