java.lang.string.split
split 方法
将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
stringObj.split([separator,[limit]])
参数
stringObj
必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator
可选项。字符串或 正则表达式对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽
略该选项,返回包含整个字符串的单一元素数组。
limit
可选项。该值用来限制返回数组中的元素个数。
说明
split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解
。separator 不作为任何数组元素的部分返回。
示例1:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ");
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.
示例2:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ",2);
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain in Spain falls mainly in the plain.
示例3:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ",20);
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain
        split 方法
将一个字符串分割为子字符串,然后将结果作为字符串数组返回。
stringObj.split([separator,[limit]])
参数
stringObj
必选项。要被分解的 String 对象或文字。该对象不会被 split 方法修改。
separator
可选项。字符串或 正则表达式对象,它标识了分隔字符串时使用的是一个还是多个字符。如果忽
略该选项,返回包含整个字符串的单一元素数组。
limit
可选项。该值用来限制返回数组中的元素个数。
说明
split 方法的结果是一个字符串数组,在 stingObj 中每个出现 separator 的位置都要进行分解
。separator 不作为任何数组元素的部分返回。
示例1:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ");
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain.
示例2:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ",2);
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain in Spain falls mainly in the plain.
示例3:
public class SplitDemo {
public static String[] ss=new String[20];
public SplitDemo() {
String s = "The rain in Spain falls mainly in the plain.";
// 在每个空格字符处进行分解。
ss = s.split(" ",20);
}
public static void main(String[] args) {
SplitDemo demo=new SplitDemo();
for(int i=0;i<ss.length;i++)
System.out.println(ss[i]);
}
}
程序结果:
The
rain
in
Spain
falls
mainly
in
the
plain
 
                   
                   
                   
                   
                             本文详细介绍了 Java 中 String 类的 split 方法用法,包括如何通过指定分隔符和限制返回数组大小来分解字符串。提供了三个示例代码,展示 split 方法在不同参数设置下的行为表现。
本文详细介绍了 Java 中 String 类的 split 方法用法,包括如何通过指定分隔符和限制返回数组大小来分解字符串。提供了三个示例代码,展示 split 方法在不同参数设置下的行为表现。
           
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   3015
					3015
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            