4

I have the following text: ARIYALUR:ARIYALUR|CHENNAI:CHENNAI|COIMBATORE:COIMBATORE|CUDDALORE:CUDDALORE|DINDIGUL:DINDIGUL|ERODE:ERODE|KANCHEEPURAM:KANCHEEPURAM|KANYAKUMARI:KANYAKUMARI|KRISHNAGIRI:KRISHNAGIRI|MADURAI:MADURAI|NAMAKKAL:NAMAKKAL|NILGIRIS:NILGIRIS|PERAMBALUR:PERAMBALUR|PONDICHERRY:PONDICHERRY|SALEM:SALEM|THANJAVUR:THANJAVUR|THENI:THENI|THIRUVALLUR:THIRUVALLUR|THOOTHUKUDI:THOOTHUKUDI|TIRUNELVELI:TIRUNELVELI|VELLORE:VELLORE|VILLUPURAM:VILLUPURAM|VIRUDHUNAGAR:VIRUDHUNAGAR|

I tried to do a split("|") but my array is made up of single characters and not each district.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
user903772
  • 1,554
  • 5
  • 32
  • 55
  • 1
    split takes a regex (regular expression), hence, as Haozhun says, it is a special character. There are others too, so be careful! – xagyg Dec 01 '12 at 16:47

3 Answers3

12

| is a special symbol in regular expression. Use \\| instead.

I'll explain why I appended 2 slashes. To escape the |, I need \|. However, to represent the string \|, "\\|" is required because \ itself needs to be escaped in a string lateral.

And, as xagyg has pointed out in the comment, split will treat the parameter as a regular expression. It will not be treated as a plain string.

In this use case, you may be interested to learn about Pattern.quote. You can do Pattern.quote("|"). This way, none of the characters will be treated as special ones.

Haozhun
  • 6,331
  • 3
  • 29
  • 50
  • @user903772 You need `"\\|"` because `\\` itself is a special symbol in String, therefore, you have to escape it. – Haozhun Dec 01 '12 at 16:54
0

You need to use the escape char before the meta char | which represnt OR. Also since you need to pass the regex in split as String, you need to escape the escape character as well.

Try below:

    String str = "ARIYALUR:ARIYALUR|CHENNAI:CHENNAI|COIMBATORE:COIMBATORE|CUDDALORE:CUDDALORE|DINDIGUL:DINDIGUL|ERODE:ERODE|KANCHEEPURAM:KANCHEEPURAM|KANYAKUMARI:KANYAKUMARI|KRISHNAGIRI:KRISHNAGIRI|MADURAI:MADURAI|NAMAKKAL:NAMAKKAL|NILGIRIS:NILGIRIS|PERAMBALUR:PERAMBALUR|PONDICHERRY:PONDICHERRY|SALEM:SALEM|THANJAVUR:THANJAVUR|THENI:THENI|THIRUVALLUR:THIRUVALLUR|THOOTHUKUDI:THOOTHUKUDI|TIRUNELVELI:TIRUNELVELI|VELLORE:VELLORE|VILLUPURAM:VILLUPURAM|VIRUDHUNAGAR:VIRUDHUNAGAR|";
    String [] tokens = str.split("\\|");
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
-2
public static String[] splitWord(String x){
    String[] j = new String [200];
    for(int i=0;i<x.split("\|").length;i++){
        j[i] = x.split("\|")[i];
    }
    return j;
}

I came up with this method for these types of situations. To use it, call the method and specify what word you need to access:

Classname.splitWord(String)[word in array];
  • 1
    That's terrible code, sorry. 1.) You're splitting the string each time you check the condition in the for loop and each time you copy the value. That's 2*n `split` calls where one would suffice. 2.) `200` as a magic number is a bad idea, what if `x` has 201 elements? 3.) Why do you *copy* the resulting array anyway? 4.) Why do you copy it manually instead of using something like `System.arraycopy` or simply `.clone()`. – Joachim Sauer Jun 05 '13 at 14:50