• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Issue with the String split method when using "|"

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have strings which use the following seperator |

If I split using "|" I get a string array with one cell for every character. If I use a different character (regex) it works. Does anyone know why? Does the | character have a special meaning in the String.

Below is sample code and output explaining the issue. Note: This uses Java1.5.

class test
{
public static void main(String[] args)
{
int i;
String s1 = "Hello| Wo|rld!";

String[] sArray1 = s1.split("|");
String[] sArray2 = s1.split("o");

System.out.println("s1 = " + s1);
System.out.println("s1.split(\"|\")");
System.out.println("sArray1.length = " + sArray1.length);

for (i = 0 ; i < sArray1.length; i++)
{
System.out.println("sArray1[" + i + "] = " + sArray1[i]);
}

System.out.println();
System.out.println("s1.split(\"o\")");
System.out.println("sArray2.length = " + sArray2.length);
for (i = 0 ; i < sArray2.length; i++)
{
System.out.println("sArray2[" + i + "] = " + sArray2[i]);
}


}
}


// Output
---------- Run ----------
s1 = Hello| Wo|rld!
s1.split("|")
sArray1.length = 15
sArray1[0] =
sArray1[1] = H
sArray1[2] = e
sArray1[3] = l
sArray1[4] = l
sArray1[5] = o
sArray1[6] = |
sArray1[7] =
sArray1[8] = W
sArray1[9] = o
sArray1[10] = |
sArray1[11] = r
sArray1[12] = l
sArray1[13] = d
sArray1[14] = !

s1.split("o")
sArray2.length = 3
sArray2[0] = Hell
sArray2[1] = | W
sArray2[2] = |rld!

Output completed (0 sec consumed) - Normal Termination
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should be s.split("\\|"); Try that.
 
Pascal Lafond
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your 100% right.

The | character means OR in a java pattern and you need to escape it. The pattern "|" that I used looked for an empty string OR an empty string.

"\\|" is the proper pattern to look for the | character.

Thanks
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic