How to use "." as delimiter in String.split() method

String.split takes a regex, and '.' has a special meaning for regexes.
You have to use "\" along with "." in arguments like below
String[] words = line.split("\\.");

Below is a sample java program explaining the use



import java.util.Arrays;

public class TestSplit {
  public static void main(String[] args) {
    String line = "aa.bb.cc.dd";
    String[] words = line.split("\\.");
    System.out.println(Arrays.toString(words));
    // Output is "[aa, bb, cc, dd]"
  }
}
SHARE

    Blogger Comment
    Facebook Comment

0 comments :

Post a Comment