-6

I have a string consisting of file separators; e.g. "Bancs\Bancs_CP_P&MB.xml".

I want to separate the string based on "\".

Here's what I'm trying:

public class Stringoperations {
    public static void main(String[] args) {
        try {
            String fileSeparator = System.getProperty("file.separator");
            String TestCaseName = "Bancs" + fileSeparator + "Bancs_CP_P&MB.xml";
            String[] tukde = TestCaseName.split("\\");
            System.out.println(tukde[0]);
            System.out.println(tukde[1]);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            System.out.println("here");
        }
    }
}

But this is not working.

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
Shweta Bet
  • 41
  • 1
  • 1
  • 8

6 Answers6

1

First: add a e.printStackTrace(); or something similar to your catch block, so you'll see what's actually wrong:

java.util.regex.PatternSyntaxException: Unexpected internal error near index 1
\
^
    at java.util.regex.Pattern.error(Pattern.java:1924)

A back slash in Java string literals allows you place special chars into a string:

String withTab = "a\tb";

would print as "a b". To get a backslash in a Java string you need to escape it:

String withBackslash = "a\\b";

So this is what you done in the split invocation: you passed one java string back slash. Since String.split() evaluates the passed string a regular expression (Java Doc for String.split()), the back slash is treated as a RegEx. Backslash has a special meaning in regular expressions and cannot appear alone (Java Doc for Pattern). If you want a literal back slash you need to escape the back slash again:

String[] tukde = TestCaseName.split("\\\\");
Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73
0

First, putting that code into IntelliJ IDEA causes it to fuss at me with an illegal escape sequence. You have to escape the escape, so you'd be using \\\\ as valid backslash escape syntax.

Second, you should be splitting on fileSeparator, not an arbitrary backslash. The backslash actually varies from system to system (e.g. I'm on Linux Mint, and my separators are all forward slashes).

String[] tukde = TestCaseName.split(fileSeparator);

As a further note, there's no exceptions here that could be thrown (save for runtime), and blindly catching all exceptions isn't a good practice.

Makoto
  • 104,088
  • 27
  • 192
  • 230
0

Try this code :-

public static void main(String[] args) {
        try
        {

        String fileSeparator = System.getProperty("file.separator");
        String TestCaseName = "Bancs"+fileSeparator+"Bancs_CP_P&MB.xml";
        String[] tukde = TestCaseName.split("\\\\");

        System.out.println(tukde[0]);
        System.out.println(tukde[1]);
        }catch(Exception e)
        {
            e.getMessage();
        }
        finally
        {
            System.out.println("here");
        }
    }

Out put :-

Bancs
Bancs_CP_P&MB.xml
here
JDGuide
  • 6,239
  • 12
  • 46
  • 64
0

What platform or OS are you working on. Maybe your default file separator is not "\". Try this :

public class Stringoperations {
    public static void main(String[] args) {
        try {
            String fileSeparator = System.getProperty("file.separator");
            String TestCaseName = "Bancs" + fileSeparator + "Bancs_CP_P&MB.xml";
            String[] tukde = TestCaseName.split(fileSeparator); //This uses the default separator returned by System.getProperty
            System.out.println(tukde[0]);
            System.out.println(tukde[1]);
        } catch (Exception e) {
            e.getMessage();
        } finally {
            System.out.println("here");
        }
    }
}

EDIT :

Also as Makoto points out if you are bent on using "\" to split you need to use "\\" and not "\"

0

Try this:

String pattern = Pattern.quote(System.getProperty("file.separator"));
String[] splittedFileName = fileName.split(pattern);
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
0
public class Stringoperations {
public static void main(String[] args) {
    try {
        String fileSeparator = System.getProperty("file.separator");
        String TestCaseName = "Bancs" + fileSeparator + "Bancs_CP_P&MB.xml";
        String[] tukde = TestCaseName.split("\\\\");
        System.out.println(tukde[0]);
        System.out.println(tukde[1]);
    } catch (Exception e) {
        e.getMessage();
    } finally {
        System.out.println("here");
    }
}
}

try this