• Post Reply Bookmark Topic Watch Topic
  • New Topic

need help on string.split  RSS feed

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


I have a method which validates IP address format.  I am passing value for ipAdr as "10.10.192.32".  First if condition is working fine.  However, I observe that, control is not going inside for loop.  Is there anything wrong in the syntax?  Please help.

As per my knowledge ipAdr.split(".")  command will give ipAdr as a list of arry like -   [10,10,192,32]  (of course string values)




Thanks,
-Anand

 
Rancher
Posts: 1815
15
Android Eclipse IDE Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
split method returns an array.
 
Greenhorn
Posts: 2
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Copy the split values into String Array and iterate
 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried like below, but still no result for the print statement inside for loop -




 
Ranch Foreman
Posts: 3068
37
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The split() method takes a regular expression.
For regexes, a '.' is a special character.
If you wan to actually split on a '.' you will need to escape it.
 
Ranch Hand
Posts: 146
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the Arrays class's toString method to Print out the array returned by split to see what is happening:
 
Gp Anand
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Dave, it worked.  I used escape for "."



 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!