在 Java 中按空格分割字符串
本文介绍了如何在 Java 中按空格分割字符串。
有几种在 Java 中拆分字符串的方法,例如 String
类的 split()
方法,StringUtils
类的 split()
方法,StringTokenizer
类,Pattern
的 compile()
方法等。
在 Java 中使用 split()
方法分割字符串
Java 提供了一种方法 split()
来根据指定的 char 拆分字符串。它是 String
类方法,在将字符串吐出后返回一个字符串数组。我们可以使用其索引值进一步访问数组中的每个字符串。
我们在 split()
方法中使用正则表达式以空格分隔字符串。请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args) {
String str = "Hello This is Jiyik";
String[] newStr = str.split("\\s+");
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
输出:
Hello
This
is
Jiyik
在 Java 中使用 split()
和 trim()
方法分割字符串
如果字符串的开头包含空格,它将返回一个数组,该数组包含第一个为空的索引。为避免此问题,我们可以使用 String
类的 trim()
方法来修剪字符串中的所有前导和后导空格,然后应用 split()
方法来获取所有结果的数组字符串。
由于它返回一个数组,因此我们应该使用 for
循环按索引遍历它的所有元素。请参见以下示例。
public class SimpleTesting{
public static void main(String[] args) {
String str = " Hello This is Jiyik";
str = str.trim();
String[] newStr = str.split("\\s+");
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
输出:
Hello
This
is
Jiyik
在 Java 中使用 split()
方法分割字符串
除了 Java String
类之外,还有另一个类 StringUtils
,它属于 Apache
库。因此,如果你使用的是 Apache
公共库,则可以使用此类及其 split()
方法来按空格分隔字符串。
此 split()
方法不使用正则表达式作为参数;它需要一个需要拆分的字符串参数。请参见以下示例。
import org.apache.commons.lang3.StringUtils;
public class SimpleTesting{
public static void main(String[] args) {
String str = "Hello This is Jiyik";
String[] newStr = StringUtils.split(str);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
输出:
Hello
This
is
Jiyik
使用 Java 中的 StringTokenizer
类拆分字符串
我们可以使用 StringTokenizer
类通过空格分割字符串。它将分割后的标记作为字符串返回。请参见下面的示例。
import java.util.StringTokenizer;
public class SimpleTesting{
public static void main(String[] args) {
String str = "Hello This is Jiyik";
StringTokenizer tokens = new StringTokenizer(str, " ");
String[] newStr = new String[tokens.countTokens()];
int index=0;
while(tokens.hasMoreTokens()){
newStr[index] = tokens.nextToken();
System.out.println(newStr[index]);
index++;
}
}
}
输出:
Hello
This
is
Jiyik
在 Java 中使用 split()
和 compile()
方法拆分字符串
compile()
方法属于 Pattern
类,然后可以使用 split()
方法获取分割字符串数组。我们使用 compile()
方法来指定分割字符。请参见下面的示例。
import java.util.regex.Pattern;
public class SimpleTesting{
public static void main(String[] args) {
String str = "Hello This is Jiyik";
final Pattern space = Pattern.compile(" ");
String[] newStr = space.split(str);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
输出:
Hello
This
is
Jiyik
在 Java 中使用 split()
方法分割字符串
String
类的 split()
方法可用于在指定的索引上拆分字符串。例如,如果我们只想分割前三个空格,我们可以简单地将此数字作为第二个参数传递给方法。请参见下面的示例。
public class SimpleTesting{
public static void main(String[] args) {
String str = "Hello This is Jiyik";
String[] newStr = str.split(" ",3);
for (int i = 0; i < newStr.length; i++) {
System.out.println(newStr[i]);
}
}
}
输出:
Hello
This
is Jiyik
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
在 Python 中将 Pandas 系列的日期时间转换为字符串
发布时间:2024/04/24 浏览次数:894 分类:Python
-
了解如何在 Python 中将 Pandas 系列日期时间转换为字符串
在 Python Pandas 中使用 str.split 将字符串拆分为两个列表列
发布时间:2024/04/24 浏览次数:1124 分类:Python
-
本教程介绍如何使用 pandas str.split() 函数将字符串拆分为两个列表列。
在 Pandas 中执行 SQL 查询
发布时间:2024/04/24 浏览次数:1195 分类:Python
-
本教程演示了在 Python 中对 Pandas DataFrame 执行 SQL 查询。
在 Pandas 中使用 stack() 和 unstack() 函数重塑 DataFrame
发布时间:2024/04/24 浏览次数:1289 分类:Python
-
本文讨论了 Pandas 中 stack() 和 unstack() 函数的使用。
在 Pandas 中读取 Excel 多张工作表
发布时间:2024/04/24 浏览次数:1450 分类:Python
-
本教程演示如何在 Pandas Python 中从 Excel 工作簿中读取多个 Excel 工作表。