0

I want to split a file with a pipe character on a string like number|twitter|abc.. in the mapper. It is a long string. But it doesn't recognize pipe delimiter when I do:

String[] columnArray = line.split("|");

If I try to split it with a space like line.split(" "), it works fine so I don't think there is a problem with it recognizing characters. Is there any other character that can look like pipe? Why doesn't split recognize the | character?

Steve P.
  • 14,489
  • 8
  • 42
  • 72
user2441441
  • 1,237
  • 4
  • 24
  • 45

2 Answers2

1

As shared in another answer "String.split expects a regular expression argument. An unescaped | is parsed as a regex meaning "empty string or empty string," which isn't what you mean." https://stackoverflow.com/a/9808719/2623158

Here's a test example.

public class Test
{
   public static void main(String[] args)
   {
      String str = "test|pipe|delimeter";
      String [] tmpAr = str.split("\\|");

      for(String s : tmpAr)
      {
         System.out.println(s);
      }
   }
}
Community
  • 1
  • 1
JBuenoJr
  • 945
  • 9
  • 14
0

String.split takes a regular expression (as the javadoc states), and "|" is a special character in regular expressions. try "[|]" instead.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118