如何分割字符串数组?

时间:2022-01-22 21:29:23

Intention is to take a current line (String that contains commas), replace white space with "" (Trim space) and finally store split String elements into the array.

目的是获取当前行(包含逗号的字符串),将空格替换为“”(修饰空格),最后将分割的字符串元素存储到数组中。

Why does not this work?

为什么不这样做呢?

String[] textLine = currentInputLine.replace("\\s", "").split(",");

4 个解决方案

#1


6  

I think you want replaceAll rather than replace.

我认为你需要的是替换,而不是替换。

And replaceAll("\\s","") will remove all spaces, not just the redundant ones. If that's not what you want, you should try replaceAll("\\s+","\\s") or something like that.

并且替换all(“\\s”,“”)将删除所有空格,而不只是多余的空格。如果这不是您想要的,您应该尝试replaceAll(“\s+”、“\s”)或类似的东西。

#2


10  

On regex vs non-regex methods

The String class has the following methods:

String类有以下方法:

So here we see the immediate cause of your problem: you're using a regex pattern in a non-regex method. Instead of replace, you want to use replaceAll.

因此,这里我们看到了您的问题的直接原因:您正在使用非regex方法中的regex模式。要使用replaceAll而不是替换。

Other common pitfalls include:

其他常见的错误包括:

  • split(".") (when a literal period is meant)
  • split(“.”)(指一个文字句号)
  • matches("pattern") is a whole-string match!
    • There's no contains("pattern"); use matches(".*pattern.*") instead
    • 没有包含(“模式”);使用匹配(”。*模式。*”)
  • match(“pattern”)是一个全字符串匹配!没有包含(“模式”);使用匹配(”。*模式。*”)

On Guava's Splitter

Depending on your need, String.replaceAll and split combo may do the job adequately. A more specialized tool for this purpose, however, is Splitter from Guava.

根据你的需要,字符串。replaceAll和split combo组合可以充分发挥作用。不过,更专门的工具是来自番石榴的Splitter。

Here's an example to show the difference:

这里有一个例子来说明差异:

public static void main(String[] args) {
    String text = "  one, two, , five (three sir!) ";

    dump(text.replaceAll("\\s", "").split(","));
    // prints "[one] [two] [] [five(threesir!)] "

    dump(Splitter.on(",").trimResults().omitEmptyStrings().split(text));
    // prints "[one] [two] [five (three sir!)] "
}

static void dump(String... ss) {
    dump(Arrays.asList(ss));
}
static void dump(Iterable<String> ss) {
    for (String s : ss) {
        System.out.printf("[%s] ", s);
    }
    System.out.println();       
}

Note that String.split can not omit empty strings in the beginning/middle of the returned array. It can omit trailing empty strings only. Also note that replaceAll may "trim" spaces excessively. You can make the regex more complicated, so that it only trims around the delimiter, but the Splitter solution is definitely more readable and simpler to use.

注意,字符串。在返回的数组的开始/中间部分不能省略空字符串。它只能省略拖尾空字符串。还要注意,replaceAll可能过度“修剪”空格。您可以使regex更加复杂,以便它只对分隔符进行修剪,但是Splitter解决方案肯定更易于阅读和使用。

Guava also has (among many other wonderful things) a very convenient Joiner.

番石榴还有一个非常方便的细木工。

System.out.println(
    Joiner.on("... ").skipNulls().join("Oh", "My", null, "God")
);
// prints "Oh... My... God"

#3


2  

What you wrote does not match the code:

你所写的与代码不符:

Intention is to take a current line which contains commas, store trimmed values of all space and store the line into the array.

意图是使用包含逗号的当前行,存储所有空间的切边值,并将该行存储到数组中。

It seams, by the code, that you want all spaces removed and split the resulting string at the commas (not described). That can be done as Paul Tomblin suggested.

根据代码,您需要删除所有空格,并在逗号(未描述)处分割结果字符串。这可以像Paul Tomblin说的那样。

String[] currentLineArray = currentInputLine.replaceAll("\\s", "").split(",");

If you want to split at the commas and remove leading and trailing spaces (trim) from the resulting parts, use:

如果你想在逗号处分开,并从产生的部件中删除前导和后尾空间(饰),请使用:

String[] currentLineArray = currentInputLine.trim().split("\\s*,\\s*");

(trim() is needed to remove leading spaces of first part and trailing space from last part)

(trim()用于去除第一部分的前导间隙和最后部分的后导间隙。)

#4


0  

If you need to perform this operation repeatedly, I'd suggest using java.util.regex.Pattern and java.util.regex.Matcher instead.

如果需要重复执行此操作,我建议使用java.util.regex。模式和java.util.regex。匹配器。

final Pattern pattern = Pattern.compile( regex);
for(String inp: inps) {
    final Matcher matcher = pattern.matcher( inpString);
    return matcher.replaceAll( replacementString); 
}

Compiling a regex is a costly operation and using String's replaceAll repeatedly is not recommended, since each invocation involves compilation of regex followed by replacement.

编译regex是一个代价高昂的操作,不建议重复使用String的replaceAll,因为每次调用都涉及编译regex,然后替换。

#1


6  

I think you want replaceAll rather than replace.

我认为你需要的是替换,而不是替换。

And replaceAll("\\s","") will remove all spaces, not just the redundant ones. If that's not what you want, you should try replaceAll("\\s+","\\s") or something like that.

并且替换all(“\\s”,“”)将删除所有空格,而不只是多余的空格。如果这不是您想要的,您应该尝试replaceAll(“\s+”、“\s”)或类似的东西。

#2


10  

On regex vs non-regex methods

The String class has the following methods:

String类有以下方法:

So here we see the immediate cause of your problem: you're using a regex pattern in a non-regex method. Instead of replace, you want to use replaceAll.

因此,这里我们看到了您的问题的直接原因:您正在使用非regex方法中的regex模式。要使用replaceAll而不是替换。

Other common pitfalls include:

其他常见的错误包括:

  • split(".") (when a literal period is meant)
  • split(“.”)(指一个文字句号)
  • matches("pattern") is a whole-string match!
    • There's no contains("pattern"); use matches(".*pattern.*") instead
    • 没有包含(“模式”);使用匹配(”。*模式。*”)
  • match(“pattern”)是一个全字符串匹配!没有包含(“模式”);使用匹配(”。*模式。*”)

On Guava's Splitter

Depending on your need, String.replaceAll and split combo may do the job adequately. A more specialized tool for this purpose, however, is Splitter from Guava.

根据你的需要,字符串。replaceAll和split combo组合可以充分发挥作用。不过,更专门的工具是来自番石榴的Splitter。

Here's an example to show the difference:

这里有一个例子来说明差异:

public static void main(String[] args) {
    String text = "  one, two, , five (three sir!) ";

    dump(text.replaceAll("\\s", "").split(","));
    // prints "[one] [two] [] [five(threesir!)] "

    dump(Splitter.on(",").trimResults().omitEmptyStrings().split(text));
    // prints "[one] [two] [five (three sir!)] "
}

static void dump(String... ss) {
    dump(Arrays.asList(ss));
}
static void dump(Iterable<String> ss) {
    for (String s : ss) {
        System.out.printf("[%s] ", s);
    }
    System.out.println();       
}

Note that String.split can not omit empty strings in the beginning/middle of the returned array. It can omit trailing empty strings only. Also note that replaceAll may "trim" spaces excessively. You can make the regex more complicated, so that it only trims around the delimiter, but the Splitter solution is definitely more readable and simpler to use.

注意,字符串。在返回的数组的开始/中间部分不能省略空字符串。它只能省略拖尾空字符串。还要注意,replaceAll可能过度“修剪”空格。您可以使regex更加复杂,以便它只对分隔符进行修剪,但是Splitter解决方案肯定更易于阅读和使用。

Guava also has (among many other wonderful things) a very convenient Joiner.

番石榴还有一个非常方便的细木工。

System.out.println(
    Joiner.on("... ").skipNulls().join("Oh", "My", null, "God")
);
// prints "Oh... My... God"

#3


2  

What you wrote does not match the code:

你所写的与代码不符:

Intention is to take a current line which contains commas, store trimmed values of all space and store the line into the array.

意图是使用包含逗号的当前行,存储所有空间的切边值,并将该行存储到数组中。

It seams, by the code, that you want all spaces removed and split the resulting string at the commas (not described). That can be done as Paul Tomblin suggested.

根据代码,您需要删除所有空格,并在逗号(未描述)处分割结果字符串。这可以像Paul Tomblin说的那样。

String[] currentLineArray = currentInputLine.replaceAll("\\s", "").split(",");

If you want to split at the commas and remove leading and trailing spaces (trim) from the resulting parts, use:

如果你想在逗号处分开,并从产生的部件中删除前导和后尾空间(饰),请使用:

String[] currentLineArray = currentInputLine.trim().split("\\s*,\\s*");

(trim() is needed to remove leading spaces of first part and trailing space from last part)

(trim()用于去除第一部分的前导间隙和最后部分的后导间隙。)

#4


0  

If you need to perform this operation repeatedly, I'd suggest using java.util.regex.Pattern and java.util.regex.Matcher instead.

如果需要重复执行此操作,我建议使用java.util.regex。模式和java.util.regex。匹配器。

final Pattern pattern = Pattern.compile( regex);
for(String inp: inps) {
    final Matcher matcher = pattern.matcher( inpString);
    return matcher.replaceAll( replacementString); 
}

Compiling a regex is a costly operation and using String's replaceAll repeatedly is not recommended, since each invocation involves compilation of regex followed by replacement.

编译regex是一个代价高昂的操作,不建议重复使用String的replaceAll,因为每次调用都涉及编译regex,然后替换。