-1

I'm working on my programming assignment, and trying to understand an issue occuring with String#split() method in Java. I've tried for 3 hours and then asking- How to split a string in Java. Here is the code snippet of it-

The Program

class FileProcessor
{
    public static void fileNameProcessor(String fileName)
    {
        System.out.println("\n\n---- PROCESSING FILE NAMES -----\n\n");
    
        fileName = "SampleName.java";                   // Let's assume the method receives this string
        String fileNameParts[] = fileName.split(".");

        for (int i = 0; i < fileNameParts.length; i++)
            System.out.println("File Information : " + fileNameParts[i]);

        System.out.println("\n\n------ END OF PROCESSING -------\n\n");
    }
}

Here is the desired output:

---- PROCESSING FILE NAMES -----


File Information : SampleName
File Information : java


------ END OF PROCESSING -------

Output that I'm getting:

---- PROCESSING FILE NAMES -----




------ END OF PROCESSING -------

I'm wondering, although the string has a dot . in it, yet the split() method is not performing its functionality. Can anyone please help me to fix this?

Aaditya
  • 1
  • 1
  • Read the [javadoc](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/String.html#split(java.lang.String)). String.split() uses regular expressions. –  Jan 21 '21 at 19:41

2 Answers2

2

Hope, This might help you the Best - Top 5 uses of String split() method in Java


The String#split() method takes Regular Expression as input.

A regular expression is not a regular string. In Regular expression- the dot (.) has a special meaning because it is a metacharacter. It tells the Java to select any character not just the literal dot .

In order to fix your code, you must Escape the dot by using two backslashes before the period or dot . Like this - "\\."

The Solution

class FileProcessor
{
    public static void fileNameProcessor(String fileName)
    {
        System.out.println("\n\n---- PROCESSING FILE NAMES -----\n\n");
    
        fileName = "SampleName.java";                     // Assume that the File Name
        String fileNameParts[] = fileName.split("\\.");   // Escaping the dot (.) by double backslash \\.

        for (int i = 0; i < fileNameParts.length; i++)
            System.out.println("File Information : " + fileNameParts[i]);

        System.out.println("\n\n------ END OF PROCESSING -------\n\n");
    }
}

Find Detailed Information Here-

Related Questions-

List of Metacharacter in Regex: There are some special characters in Regular expression with some special meanings: the asterisk or star *, the vertical bar or pipe symbol |, the question mark ?, the backslash \, the caret ^, the dollar sign $, the period or dot . , the plus sign +, and others

That's all about How to split a string in Java using split() method and Don't forget to use double the double backslash \\ whenever splitting with a metacharacter.

SKL
  • 103
  • 6
0

You will need to escape the . character.

String fileNameParts[] = fileName.split("\\.");
Jagrut Sharma
  • 4,574
  • 3
  • 14
  • 19