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