1.split()方法介绍
语法:
public String split(String regex)
public String split(String regex, int limit)
参数:
regex : 应用于字符串的正则表达式。
limit :数组中字符串的数量限制。如果它为零,它将返回所有匹配正则表达式的字符串。
返回值:array of strings
例外: PatternSyntaxException 如果正则表达式的模式无效
2.代码案例
2.1 空格
给定的示例返回字符串中的单词总数,仅不包括空格。它还包括特殊字符
public class Test {
public static void main(String[] args) {
String s1="java string split method";
// String[] words=s1.split("\\s");
String[] words=s1.split(" ");//两种方法都可以,根据空格拆分字符串
for(String w:words){
System.out.println(w);
}
}
}
java
string
split
method
2.2 指定字符串数量限制
- 限制设置为零:这将排除尾随的空字符串。
- 将限制指定为 1,因此 split 方法仅返回两个子字符串。
- 将限制指定为 2,因此 split 方法仅返回两个子字符串。
- 当 limit 为负数时,它会在输出中包含尾随的空字符串。
public class Test {
public static void main(String[] args) {
String s1 = "hello/hi/bye///";
for (String w : s1.split("/", 0)) {//限制设置为零:这将排除尾随的空字符串。
System.out.println("returning words:"+w);
}
System.out.println("------------------");
for (String w : s1.split("/", 1)) {//将限制指定为 1,因此 split 方法仅返回两个子字符串。
System.out.println("returning words:"+w);
}
System.out.println("------------------");
for (String w : s1.split("/", 2)) {//将限制指定为 2,因此 split 方法仅返回两个子字符串。
System.out.println("returning words:"+w);
}
System.out.println("------------------");
int i=1;
for (String w : s1.split("/", -1)) {//当 limit 为负数时,它会在输出中包含尾随的空字符串。
System.out.print(i++ +". ");
System.out.println(w);
}
}
}
returning words:hello
returning words:hi
returning words:bye
------------------
returning words:hello/hi/bye///
------------------
returning words:hello
returning words:hi/bye///
------------------
1. hello
2. hi
3. bye
4.
5.
6.
2.3 多个定界符
具有多个定界符的 Java String split() 方法
public class Test {
public static void main(String[] args) {
String s = " ,ab;gh,bc;pq#kk$bb";
String[] str = s.split("[,;#$]");
//Total how many substrings? The array length
System.out.println("Number of substrings: "+str.length);
for (int i=0; i < str.length; i++) {
System.out.println("Str["+i+"]:"+str[i]);
}
}
}
Number of substrings: 7
Str[0]:
Str[1]:ab
Str[2]:gh
Str[3]:bc
Str[4]:pq
Str[5]:kk
Str[6]:bb
2.4 字符串拆分为字符数组
将字符串拆分为字符数组文章来源:https://www.toymoban.com/news/detail-612054.html
public class Test {
public static void main(String[] args) {
String s = " ,ab;gh,bc;pq#kk$bb";
String[] str = s.split("(?!^)");
System.out.println("Number of substrings: "+str.length);
for (int i=0; i < str.length; i++) {
System.out.println("Str["+i+"]:"+str[i]);
}
}
}
Number of substrings: 19
Str[0]:
Str[1]:,
Str[2]:a
Str[3]:b
Str[4]:;
Str[5]:g
Str[6]:h
Str[7]:,
Str[8]:b
Str[9]:c
Str[10]:;
Str[11]:p
Str[12]:q
Str[13]:#
Str[14]:k
Str[15]:k
Str[16]:$
2.5 大写字母拆分字符串
按大写字母拆分字符串文章来源地址https://www.toymoban.com/news/detail-612054.html
public class Test {
public static void main(String[] args) {
String s = "AB#K$bb";
String[] str = s.split("(?=\\p{Lu})");
System.out.println("Number of substrings: "+str.length);
for (int i=0; i < str.length; i++) {
System.out.println("Str["+i+"]:"+str[i]);
}
}
}
Number of substrings: 3
Str[0]:A
Str[1]:B#
Str[2]:K$bb
到了这里,关于Java中split方法详细讲解的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!