Forums Register Login

problem in string's split method

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Try putting \\ in front of . : "\\."
+Pie Number of slices to send: Send
This would also work:
String [] st1=st.split("[.]");
+Pie Number of slices to send: Send
You might be thinking of StringTokenizer which can split a string into tokens based on a single value like a "."
+Pie Number of slices to send: Send
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 didn't do it. You can't prove it. Nobody saw me. The sheep are lying! This tiny ad is my witness!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 857 times.
Similar Threads
How we can Count the no.of words in a given String
Merge and split file
splitting arrays
Stack
How about with java.util.Stack??
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:01:45.