• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

split method in string

 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20881
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Saathvik Reddy
Ranch Hand
Posts: 228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Huh! it worked.
Thanks Henry.
 
Piet Verdriet
Ranch Hand
Posts: 266
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Henry Wong:


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


Except new line characters (by default).
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic