[JDK1.4]
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
String s3 = "Real-How-To";
String [] temp = null;
temp = s3.split("-");
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real
How
To
------------
*/
split() is based on regex expression, a special attention is needed with some characters which have a special meaning in a regex expression.
For example :
String s3 = "Real.How.To";
...
temp = s3.split("\\.");
or
String s3 = "Real|How|To";
...
temp = s3.split("\\|");
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
String s = "Real|How|To|||";
String [] temp = null;
temp = s.split("\\|");
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real
How
To
------------
*/
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
String s = "Real|How|To|||";
String [] temp = null;
temp = s.split("\\|", -1);
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real
How
To
------------
*/
With previous version, java.util.StringTokeniser can be used.
See this HowTo
Some notes from A. Gonzales about String.split()
An interesting thing about String.split():
" s".split(" ") -> {"","","s"}
"".split("" ) -> {""}
" ".split(" ") -> {} (!)
" ".split(" ") -> {} (!)
" s ".split(" ") -> {"","","s"} (!)
param = req.getParam(...);
String[] words = param.split(" ");
String firstWord = words[0];
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
// extra spaces
String s3 = "Real How To";
String [] temp = null;
temp = s3.split(" ");
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real
How
To
------------
*/
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
// extra spaces
String s3 = "Real How To";
String [] temp = null;
temp = s3.split("\\s+");
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
Real
How
To
------------
*/
public class StringSplit {
public static void main(String args[]) throws Exception{
new StringSplit().doit();
}
public void doit() {
String s3 = "{RealHowto}{java-0438.html}{usage of String.split()}";
String[] temp = s3.split("[{}]");
dump(temp);
}
public void dump(String []s) {
System.out.println("------------");
for (int i = 0 ; i < s.length ; i++) {
System.out.println(s[i]);
}
System.out.println("------------");
}
}
/*
output :
------------
RealHowto
java-0438.html
usage of String.split()
------------
note : extra element with empty string :-(
*/
Written and compiled by Réal Gagnon ©1998-2010
[ home ]