The split() method in Java does not work on a dot (.)

JavaStringSplit

Java Problem Overview


I have prepared a simple code snippet in order to separate the erroneous portion from my web application.

public class Main {

    public static void main(String[] args) throws IOException {
        System.out.print("\nEnter a string:->");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String temp = br.readLine();

        String words[] = temp.split(".");

        for (int i = 0; i < words.length; i++) {
            System.out.println(words[i] + "\n");
        }
    }
}

I have tested it while building a web application JSF. I just want to know why in the above code temp.split(".") does not work. The statement,

System.out.println(words[i]+"\n"); 

displays nothing on the console means that it doesn't go through the loop. When I change the argument of the temp.split() method to other characters, It works just fine as usual. What might be the problem?

Java Solutions


Solution 1 - Java

java.lang.String.split splits on regular expressions, and . in a regular expression means "any character".

Try temp.split("\\.").

Solution 2 - Java

The documentation on split() says:

> Splits this string around matches of the given regular expression.

(Emphasis mine.)

A dot is a special character in regular expression syntax. Use Pattern.quote() on the parameter to split() if you want the split to be on a literal string pattern:

String[] words = temp.split(Pattern.quote("."));

Solution 3 - Java

Try:

String words[]=temp.split("\\.");

The method is:

String[] split(String regex) 

"." is a reserved char in regex

Solution 4 - Java

The method takes a regular expression, not a string, and the dot has a special meaning in regular expressions. Escape it like so split("\\."). You need a double backslash, the second one escapes the first.

Solution 5 - Java

\\. is the simple answer. Here is simple code for your help.

while (line != null) {
    //		       
    String[] words = line.split("\\.");
    wr = "";
    mean = "";
    if (words.length > 2) {
        wr = words[0] + words[1];
        mean = words[2];

    } else {
        wr = words[0];
        mean = words[1];
    }
}

Solution 6 - Java

It works fine. Did you read the documentation? The string is converted to a regular expression.

. is the special character matching all input characters.

As with any regular expression special character, you escape with a \. You need an additional \ for the Java string escape.

Solution 7 - Java

    private String temp = "mahesh.hiren.darshan";
    
    String s_temp[] = temp.split("[.]");

  Log.e("1", ""+s_temp[0]);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBhaveshView Question on Stackoverflow
Solution 1 - Javarob mayoffView Answer on Stackoverflow
Solution 2 - JavamillimooseView Answer on Stackoverflow
Solution 3 - JavaysrbView Answer on Stackoverflow
Solution 4 - JavambatchkarovView Answer on Stackoverflow
Solution 5 - JavaIsrar KhanView Answer on Stackoverflow
Solution 6 - JavaMatthew FlaschenView Answer on Stackoverflow
Solution 7 - JavaHiren PatelView Answer on Stackoverflow