Splitting string using period or dot or any basic symbol in Java

Have you tried splitting a string in Java using the period or dot (.) with the inbuilt method split. On trying str.split(“.”) will give you an array of zero elements. This is because unlike Javascript, java wants a regular expression as the parameter for split method. You can solve this simply by giving regular expression instead of simple ‘.’ like this :

String fieldId = “1.3.0.1.i”;
String[] s = fieldId.split(“\\.”);
System.out.println(“Length of splitted array :: ” + s.length);

The output will show you Length of splitted array :: 5

The same expression applies to comma (,) as well as pipe symbol also  (|).

Just scribbled down the issue I just faced, hope this will come to help for someone like me also.

Happy Coding Guys… 🙂

Advertisements

Tags: , , , , , ,

3 Responses to “Splitting string using period or dot or any basic symbol in Java”

  1. Vektor Dewanto Says:

    So many thanks to you.

  2. Drew Spencer (@slugmandrew) Says:

    Thanks a lot! This was a bit confusing for the last half an hour to say the least! It should throw some kind of error or message to tell the user what’s going on here.

  3. sureshk86 Says:

    Thanks a lot frnd!. Your tutorial solved my issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: