迹忆客 专注技术分享

当前位置:主页 > 学无止境 > 编程语言 > Java >

在 Java 中按空格分割字符串

作者:迹忆客 最近更新:2023/04/05 浏览次数:

本文介绍了如何在 Java 中按空格分割字符串。

有几种在 Java 中拆分字符串的方法,例如 String 类的 split() 方法,StringUtils 类的 split() 方法,StringTokenizer 类,Patterncompile() 方法等。


在 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

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

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

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便