文章目录
- 一、 split函数详解
- 二、应用
一、 split函数详解
split(String regex)为java.lang.String类的方法,其功能通俗的说就是以传入的分隔符参数拆分该字符串
方法具体为:
public String[] split(String regex) {
return split(regex, 0);
}
方法内部调用了一个重载的 split(regex,0) 方法,split(regex)方法最终返回一个字符串数组。方法 split(regex,0)原形是split(String regex, int limit),该方法中的regex为匹配样板,通俗的说就是拆分字符串的标志,而limit在官方文档中是这样描述的:
The parameter controls the number of times the pattern is applied and therefore affects the length of the resulting array. If the limit n is greater than zero then the pattern will be applied at most n - 1 times, the array’s length will be no greater than n, and the array’slast entry will contain all input beyond the last matched delimiter.
If n is non-positive then the pattern will be applied as many times as possible and the array can have any length. If n is zero then the pattern will be applied as many times as possible, the array can have any length, and trailing empty strings will be discarded.
意思是说limit这个参数控制样板应用的次数,它影响结果数组的长度。如果 limit > 0,样板会被应用limit - 1次,也就是拆分字符串成 limit 部分,数组的最后一个元素会包含从最后一个样板匹配位置的下一个字符到字符串结束的所有字符;如果 limit = 0,那么输入字符串末尾的空格会被丢弃;如果 limit <= 0,样板会被尽可能地匹配,结果数组的长度是未知的
下面我们通过代码来说明上述内容
public static void main(String[] args) {
String str2 = "a,b,c,,,";
String[] str1Ayyay = str2.split("," ,2);
for(String str : str1Ayyay)
System.out.print("\""+str+"\"" +"\t");
System.out.println();
String[] str2Ayyay = str2.split(",");
for(String str : str2Ayyay)
System.out.print("\""+str+"\""+"\t");
System.out.println();
String[] str3Ayyay = str2.split(",",-1);
for(String str : str3Ayyay)
System.out.print("\""+str+"\""+"\t");
}
public static void main(String[] args) {
String str1 = "a,b,c";
String str2 = "a,b,c,,,";
String str3 = "a,b,c,,,d";
String[] str1Ayyay = str1.split(",");
for(String str : str1Ayyay)
System.out.print("\""+str+"\"" +"\t");
System.out.println();
String[] str2Ayyay = str2.split(",");
for(String str : str2Ayyay)
System.out.print("\""+str+"\""+"\t");
System.out.println();
String[] str3Ayyay = str3.split(",");
for(String str : str3Ayyay)
System.out.print("\""+str+"\""+"\t");
}
总的来说:如果 limit > 0,将字符串分为 limit 部分,返回的数组中最后一个元素是由第 limit-1 个分隔符后的所有字符组成,如果 limit = 0,将字符串末尾由相邻的分隔符分割成的空串舍去,如果 limit <= 0,将所有分隔符都应用,即分隔符都起作用, 相邻字符分割出的是一个空的字符串。
二、应用
验证IP地址
private boolean isIpv4(String IP){
String[] IP1 = IP.split("\\.");
if(IP1.length != 4)
return false;
for(String str : IP1){
//两个点相连
if(str.length() == 0)
return false;
//比较数字位数及不能有前导零的情况
if(str.length() < 0 || str.length() > 3 || (str.charAt(0) == '0' && str.length() > 1))
return false;
//比较字符串中的字符是否是数字
int num = 0;
for(int i = 0;i < str.length(); i++){
char ch = str.charAt(i);
if(ch < '0' || ch > '9')
return false;
//是数字,判断范围
num = num*10+ch-'0';
if(num < 0 || num > 255)
return false;
}
}
return true;
}
private boolean isIpv6(String IP){
String[] IP1 = IP.split(":",-1);
//判断个数
if(IP1.length != 8)
return false;
//判断字符串
for(String str : IP1){
//两个:相连
if(str.length() == 0)
return false;
if(str.length() > 4)
return false;
//判断字符串中的字符
for(int i = 0; i < str.length(); i++){
char ch = str.charAt(i);
boolean bool = (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F');
if(!bool)
return false;
}
}
return true;
}
public String solve (String IP) {
if(isIpv4(IP))
return "IPv4";
if(isIpv6(IP))
return "IPv6";
return "Neither";
}