How
to Use String.Split mehod in java
In java split mothod is woking with the
characters a-z and numbers 0-9 is good.
Using
single character:
Example:
package com.javanotes2all.java.string;
public class Split_String
{
public static void main(String[] args)
{
String s="com.javanotes2all_$_java.developer";
/**
* Split with characters
*/
System.out.println("=============split using character");
String[] s1=s.split("j");
System.out.println(s1.length);
for(int i=0;i<s1.length;i++)
System.out.println(s1[i]);
/**
* Split with numbers
*/
System.out.println("==============split using number");
String[] s2=s.split("2");
System.out.println(s2.length);
for(int i=0;i<s2.length;i++)
System.out.println(s2[i]);
}
}
output:
=============split
using character
3
com.
avanotes2all_$_
ava.developer
==============split
using number
2
com.javanotes
all_$_java.developer
Using
multiple characters:
Example:
package
com.javanotes2all.java.string;
public
class
Split_String
{
public
static
void
main(String[] args)
{
String
s="com.javanotes2211all_$_java.developer";
/**
* Split with characters
*/
System.out.println("=============split
using characters");
String[]
s1=s.split("java");
System.out.println(s1.length);
for(int
i=0;i<s1.length;i++)
System.out.println(s1[i]);
/**
* Split with numbers
*/
System.out.println("==============split
using numbers");
String[]
s2=s.split("211");
System.out.println(s2.length);
for(int
i=0;i<s2.length;i++)
System.out.println(s2[i]);
}
}
Output:
=============split
using characters
3
com.
notes2211all_$_
.developer
==============split
using numbers
2
com.javanotes2
all_$_java.developer
Useing
Special Characters:
But the problem
with special characters like $ , . etc
Example:
package
com.javanotes2all.java.string;
public
class
Split_String
{
public
static
void
main(String[] args)
{
String
s="com.javanotes2all_$_java.developer";
/**
* Split with characters
*/
System.out.println("=============split
using single special character");
String[]
s1=s.split(".");
System.out.println("array
length:="+s1.length);
for(int
i=0;i<s1.length;i++)
System.out.println(s1[i]);
/**
* Split with numbers
*/
System.out.println("==============split
using multiple special characters");
String[]
s2=s.split("_$_");
System.out.println("array
length:="+s2.length);
for(int
i=0;i<s2.length;i++)
System.out.println(s2[i]);
}
}
output:
=============split
using single special character
array
length:=0
==============split
using multiple special characters
array
length:=1
com.javanotes2all_$_java.developer
Which
is wrong output
solution:
Use
escaple sequence.
Example:
package
com.javanotes2all.java.string;
public
class
Split_String
{
public
static
void
main(String[] args)
{
String
s="com.javanotes2all_$_java.developer";
/**
* Split with characters
*/
System.out.println("=============split
using single special character");
String[]
s1=s.split("\\.");
System.out.println("array
length:="+s1.length);
for(int
i=0;i<s1.length;i++)
System.out.println(s1[i]);
/**
* Split with numbers
*/
System.out.println("==============split
using multiple special characters");
String[]
s2=s.split("\\_\\$\\_");
System.out.println("array
length:="+s2.length);
for(int
i=0;i<s2.length;i++)
System.out.println(s2[i]);
}
}
output:
=============split
using single special character
array
length:=3
com
javanotes2all_$_java
developer
==============split
using multiple special characters
array
length:=2
com.javanotes2all
java.developer
0 comments:
Post a Comment