【问题标题】:My String.split() function is not running properly and I don't know why? [duplicate]我的 String.split() 函数运行不正常,我不知道为什么? [复制]
【发布时间】:2020-11-01 14:45:57
【问题描述】:
public class fake {
    public static void main (String[] args) {
      String s = "i.like.this.program.very.much";
      String arr[] = s.split(".");
      int n = arr.length;
              
      System.out.println(n);
    }

我的输出是'0'

【问题讨论】:

    标签: java arrays string split


    【解决方案1】:

    你需要对点(.)字符进行转义,为什么你需要这样做是因为(.)是Java正则表达式中的特殊字符

              String s = "i.like.this.program.very.much";
              String arr[] = s.split("\\.");
              int n = arr.length;
                      
              System.out.println(n);
    

    另一种方式可能是:

          String s = "i.like.this.program.very.much";
          String arr[] = s.split("[.]");
          int n = arr.length;
                  
          System.out.println(n);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-07
      • 2023-02-03
      • 2022-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多