【问题标题】:Unable to remove leading spaces when doing String .split()执行 String .split() 时无法删除前导空格
【发布时间】:2020-12-08 02:30:04
【问题描述】:

我有一个String [81, 82, 83, 84, 85, 86, 87, 88, 89, 90],我正在尝试使用所有值创建字符串数组。

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

我将数组中的第一个元素作为空格,其余所有元素都很好。因此,我的数组中有 11 个项目,而不是 10 个。

在对数组进行 sysout 迭代时,我得到以下值。您可以看到我将第一个值设为空/空格。

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

【问题讨论】:

  • 注意这里 postId 是一个字符串

标签: java arrays split


【解决方案1】:

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

然后使用第 1 组中的值,并以逗号分隔,后跟 1 个或多个水平空白字符。

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

Regex demo | Java demo

例如

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]

【讨论】:

    【解决方案2】:

    你可以使用 substring 去掉方括号:

    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));
    
        }
    }
    

    【讨论】:

      【解决方案3】:

      我会为此任务使用正则表达式。

      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);
      }
      

      【讨论】:

        【解决方案4】:

        首先,将[] 替换为"",然后使用正则表达式将其拆分,\\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]
        

        【讨论】:

        • 别忘了Java中的String是不变的;所以使用这个解决方案你可以创建许多不可用的字符串。
        • @oleg.cherednik - 您的评论似乎无关紧要。 many not usable string 是什么意思?
        • 完成!我一生都在寻找答案,今天是我第一次发布问题并在3分钟内得到答案。 Stackoverflow 是真实的
        猜你喜欢
        • 2013-01-14
        • 1970-01-01
        • 2015-02-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-07
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多