无法在执行String .split()时去除前导空格。

huangapple go评论60阅读模式
英文:

Unable to remove leading spaces when doing String .split()

问题

我有一个 String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90],我试图从中创建一个包含所有值的字符串数组。

String[] array = postId.split(""[\\]\\,\\[\\s]+"");

我得到的数组中第一个元素是空格,其余所有元素都是正确的。因此,我得到的数组中有 11 个项目,而不是 10 个。

当在数组上进行迭代的 sysout 时,我得到以下值。您可以看到我得到的第一个值是空的/空格。

0 索引数组中的项目 = 
1 索引数组中的项目 = 81
2 索引数组中的项目 = 82
3 索引数组中的项目 = 83
4 索引数组中的项目 = 84
5 索引数组中的项目 = 85
6 索引数组中的项目 = 86
7 索引数组中的项目 = 87
8 索引数组中的项目 = 88
9 索引数组中的项目 = 89
10 索引数组中的项目 = 90
英文:

I have a String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90] and I am trying to create String array out of this with all the values.

String[] array = postId.split("[\\]\\,\\[\\s]+"); 

I am getting the first element in the array as space, rest all the elements are fine. Therefore, I am getting 11 items in my array rather than 10.

When doing sysout iterating over array I am getting the following value. You can see I am getting the first value as empty/space.

0 index item in array = 
1 index item in array = 81
2 index item in array = 82
3 index item in array = 83
4 index item in array = 84
5 index item in array = 85
6 index item in array = 86
7 index item in array = 87
8 index item in array = 88
9 index item in array = 89
10 index item in array = 90

答案1

得分: 2

首先,将 [] 替换为 "",然后使用正则表达式 ,\\s* 进行拆分。

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String str = ""[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]"";
        String arr[] = str.replace(""["", """").replace(""]"", """").split(",\\s*");
        System.out.println(Arrays.toString(arr));
    }
}

输出结果:

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
英文:

First, replace [ and ] with "" and then split it using the regex, ,\\s*

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
		String str = "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";
		String arr[] = str.replace("[", "").replace("]", "").split(",\\s*");
		System.out.println(Arrays.toString(arr));
	}
}

Output:

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]

答案2

得分: 2

我会在这个任务中使用正则表达式。

public static String[] extractNumbers(String str) {
    Pattern pattern = Pattern.compile("\\d+");
    Matcher matcher = pattern.matcher(str);
    List<String> numbers = new ArrayList<>();

    while (matcher.find()) {
        numbers.add(matcher.group(0));
    }

    return numbers.toArray(String[]::new);
}
英文:

I would use regular expression for this task.

public static String[] extractNumbers(String str) {
    Pattern pattern = Pattern.compile(&quot;\\d+&quot;);
    Matcher matcher = pattern.matcher(str);
    List&lt;String&gt; numbers = new ArrayList&lt;&gt;();

    while (matcher.find()) {
        numbers.add(matcher.group(0));
    }

    return numbers.toArray(String[]::new);
}

答案3

得分: 2

可以使用子字符串方法去掉方括号:

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
      String postId = "[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";
      String[] array = postId.substring(1, postId.length()-1).split("[\\]\\,\\[\\s]+"); 
      System.out.println(Arrays.toString(array));

    }
}
英文:

You can use substring to get rid of the square brackets:

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
      String postId = &quot;[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]&quot;;
      String[] array = postId.substring(1, postId.length()-1).split(&quot;[\\]\\,\\[\\s]+&quot;); 
      System.out.println(Arrays.toString(array));

    }
}

答案4

得分: 0

获取示例字符串的精确格式的数字,您可以使用模式首先匹配格式,并在第1组中捕获逗号分隔的值。

然后使用来自第1组的值,在逗号后跟1个或多个水平空白字符的情况下进行拆分。

\bString\h+\[([0-9]+(?:,\h+[0-9]+)*)\]

正则表达式演示 | Java 演示

例如:

String regex = "\\bString\\h+\\[([0-9]+(?:,\\h+[0-9]+)*)\\]";
String string = "String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90]";

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

if (matcher.find()) {
    String[] array = matcher.group(1).split(",\\h+");
    System.out.println(Arrays.toString(array));
}

输出:

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]
英文:

To get the numbers for the exact format of the example string, you could use a pattern to first match the format, and capture the comma separated value in group 1.

Then use the value from group 1, and split on a comma followed by 1 or more horizontal whitespace chars.

\bString\h+\[([0-9]+(?:,\h+[0-9]+)*)\]

Regex demo | Java demo

For example

String regex = &quot;\\bString\\h+\\[([0-9]+(?:,\\h+[0-9]+)*)\\]&quot;;
String string = &quot;String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90]&quot;;

Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(string);

if (matcher.find()) {
    String[] array = matcher.group(1).split(&quot;,\\h+&quot;);
    System.out.println(Arrays.toString(array));
}

Output

[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]

huangapple
  • 本文由 发表于 2020年8月19日 04:58:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63476509.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定