本文共 4763 字,大约阅读时间需要 15 分钟。
java字符串拆分 空字符
Java String split() method is used to split the string into a string array based on the provided regular expression.
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中拆分字符串。
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]
。 请注意,最后一个空字符串从返回的字符串数组中排除。
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]); //PankajSystem.out.println("Address = "+data[1]); //New York,USA
public String[] split(String regex, int limit)
:当我们希望将字符串拆分为有限数量的字符串时,可以使用此java字符串拆分方法。 例如,假设我们有一个String变量,其中包含名称和地址,并以逗号作为分隔符。 现在,该地址中可以包含逗号,因此我们可以使用此方法。 下面是该字符串拆分的简短示例。 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);}
Some important points about java String split method are:
关于Java字符串拆分方法的一些重要点是:
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为零,则该模式将被尽可能多地应用,该数组可以具有任意长度,并且尾随的空字符串将被丢弃。
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 special characters as normal characters like I have used pipe (|) special character in the above program.
我们可以使用反斜杠将特殊字符用作普通字符,就像我在上述程序中使用竖线(|)特殊字符一样。
That’s all for a quick roundup on java string split example.
这就是对Java字符串拆分示例的快速汇总。
Reference:
参考:
翻译自:
java字符串拆分 空字符
转载地址:http://omozd.baihongyu.com/