Can't do str.split("|") ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spoken
    New Member
    • Oct 2007
    • 15

    Can't do str.split("|") ?

    Hi,

    I'm trying to read a file with data seperated by "|" character.
    Ex:
    3578.27|2001|Ro ad Bikes|Metchosin |Canada
    3399.99|2001|Mo untain Bikes|Pantin|Fr ance
    3399.99|2001|Mo untain Bikes|Lebanon|U nited States

    After reading the first line, I'm trying to split the string into an array by doing
    Code:
    String[] x = str.split("|");
    However java see "|" as an alternation rather than a regex delimiter for my string.

    And I cannot change the data file because it has over 3000 lines.

    Regards,
    Gordon
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    The '|' character is special to the regular expression compiler so you should escape
    it with a '\' (backslash), but then again the backslash is also special to the Java
    compiler (javac) so you should escape it twice; use "\\|"

    kind regards,

    Jos

    Comment

    • hirak1984
      Contributor
      • Jan 2007
      • 316

      #3
      If the trailing fields are optional, you need to write:
      [CODE=Java]line.split("\\| ",-1);[/CODE]

      demo:
      [CODE=Java]import java.util.Array s;

      public class Splitsville {
      public static void main(String[] args) {
      String[] data = {
      "a|b|c|d|e" ,
      "||||",
      "||c||"
      };
      for(String line : data) {
      String[] tokens = line.split("\\| ");
      System.out.prin tln("split: " + Arrays.toString (tokens));
      String[] tokens2 = line.split("\\| ",-1);
      System.out.prin tln("split with -1: " + Arrays.toString (tokens2));
      }
      }
      }[/CODE]
      Last edited by BigDaddyLH; Feb 18 '08, 03:48 PM. Reason: woops! editting post by mistake, but then it was bad advice anyway ;-)

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by spoken
        Hi,

        I'm trying to read a file with data seperated by "|" character.
        Ex:
        3578.27|2001|Ro ad Bikes|Metchosin |Canada
        3399.99|2001|Mo untain Bikes|Pantin|Fr ance
        3399.99|2001|Mo untain Bikes|Lebanon|U nited States

        After reading the first line, I'm trying to split the string into an array by doing
        Code:
        String[] x = str.split("|");
        However java see "|" as an alternation rather than a regex delimiter for my string.

        And I cannot change the data file because it has over 3000 lines.

        Regards,
        Gordon
        Escape the special character using \\
        i.e split("\\|");

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by hirak1984
          first of all, this is not a solution. It is a suggestion rather.

          Code:
          String str = "3578.27|2001|Road Bikes|Metchosin|Canada";
          		String str1 = str.replace("|", ",");
          		String[] x = str1.split("'");
          		int i=0;
          		while (i<x.length){
          		System.out.println(x[i++]);
          		}
          What if there are words with ' in them?

          Comment

          • hirak1984
            Contributor
            • Jan 2007
            • 316

            #6
            actually this works:
            Code:
            Str.split("\\|");

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by hirak1984
              actually this works:
              Code:
              Str.split("\\|");
              Yes. That's the third time that that is being said. The explanation is in the first reply.

              Comment

              • spoken
                New Member
                • Oct 2007
                • 15

                #8
                Thank you all for your replies.

                Yes,
                str.split("\\|" );
                works perfectly.

                Thanks again.. :D

                Comment

                Working...