java字符串拆分 空字符_Java字符串拆分

java字符串拆分 空字符

Java String split() method is used to split the string into a string array based on the provided regular expression.

Java String split()方法用于根据提供的正则表达式将字符串拆分为字符串数组。

Java字符串拆分 (Java String split)

Sometimes we have to split String in Java, for example splitting data of CSV file to get all the different values.

有时我们必须在Java中拆分String,例如拆分CSV文件的数据以获取所有不同的值。

String class provides two useful methods to split a string in java.

字符串类提供了两种有用的方法来在Java中拆分字符串。

  1. public String[] split(String regex): This java string split method is used to split the string based on the given regular expression string. The whole string is split and returned in the form of a string array. This method was introduced in Java 1.4. Notice that the trailing empty strings are not included in the returned string array. Let’s look at a simple java string split example to understand this.
    String s = "abcaada";
    System.out.println(Arrays.toString(s.split("a")));

    Above code will produce output as [, bc, , d]. Notice that the last empty string is excluded from the returned string array.

    public String[] split(String regex) :此java字符串split方法用于根据给定的正则表达式字符串拆分字符串。 整个字符串被拆分并以字符串数组的形式返回。 此方法是Java 1.4中引入的。 请注意,返回的字符串数组中不包含结尾的空字符串。 让我们看一个简单的java字符串拆分示例以了解这一点。

    上面的代码将输出为[, bc, , d] 。 请注意,最后一个空字符串从返回的字符串数组中排除。

  2. public String[] split(String regex, int limit): This java string split method is used when we want the string to be split into a limited number of strings. For example, let’s say we have a String variable that contains name and address with comma as a delimiter. Now, the address can have commas in them, so we can use this method. A short example of this string split is given below.
    String s = "Pankaj,New York,USA";
    String[] data = s.split(",", 2);
    System.out.println("Name = "+data[0]); //Pankaj
    System.out.println("Address = "+data[1]); //New York,USA

    public String[] split(String regex, int limit) :当我们希望将字符串拆分为有限数量的字符串时,可以使用此java字符串拆分方法。 例如,假设我们有一个String变量,其中包含名称和地址,并以逗号作为分隔符。 现在,该地址中可以包含逗号,因此我们可以使用此方法。 下面是该字符串拆分的简短示例。
  3. Note that the first method above actually utilizes the second method by passing limit as 0.

    请注意,上述第一种方法实际上是通过将limit传递为0来利用第二种方法。

    public String[] split(String regex) {
        return split(regex, 0);
    }

Java String拆分要点 (Java String split important points)

Some important points about java String split method are:

关于Java字符串拆分方法的一些重要点是:

  • The array returned by this method contains each substring of this string that is terminated by another substring that matches the given expression or is terminated by the end of the string. The substrings in the array are in the order in which they occur in this string.

    此方法返回的数组包含此字符串的每个子字符串,该子字符串由与给定表达式匹配的另一个子字符串终止或由字符串的结尾终止。 数组中的子字符串按照它们在此字符串中出现的顺序排列。
  • If the regex doesn’t match any part of the input string then the resulting array has just one element, namely this string.

    如果正则表达式与输入字符串的任何部分都不匹配,则结果数组只有一个元素,即此字符串。
  • The limit 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 time, the array’s length will be no greater than n, and the array’s last 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.

    如果n为非正数,则将尽可能多地应用该模式,并且数组可以具有任何长度。

    如果n为零,则该模式将被尽可能多地应用,该数组可以具有任意长度,并且尾随的空字符串将被丢弃。

Java字符串拆分示例 (Java String split example)

Here is an example of java String split method.

这是java字符串拆分方法的示例。

package com.journaldev.util;

import java.util.Arrays;

public class JavaStringSplit {

	/**
	 * Java String split example
	 * 
	 * @param args
	 */
	public static void main(String[] args) {
		String line = "I am a java developer";

		String[] words = line.split(" ");

		String[] twoWords = line.split(" ", 2);

		System.out.println("String split with delimiter: " + Arrays.toString(words));

		System.out.println("String split into two: " + Arrays.toString(twoWords));

		// split string delimited with special characters
		String wordsWithNumbers = "I|am|a|java|developer";

		String[] numbers = wordsWithNumbers.split("\\|");

		System.out.println("String split with special character: " + Arrays.toString(numbers));

	}
}

Below image shows the output produced by the above String split example program.

下图显示了上面的String split示例程序产生的输出。

We can use a backslash to use java regular expression special characters as normal characters like I have used pipe (|) special character in the above program.

我们可以使用反斜杠将Java正则表达式特殊字符用作普通字符,就像我在上述程序中使用竖线(|)特殊字符一样。

That’s all for a quick roundup on java string split example.

这就是对Java字符串拆分示例的快速汇总。

Reference: API Doc

参考: API文档

翻译自: https://www.journaldev.com/791/java-string-split

java字符串拆分 空字符

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值