【问题标题】:Split a string into different parts with dynamic length [duplicate]将字符串拆分为具有动态长度的不同部分[重复]
【发布时间】:2021-09-10 04:40:34
【问题描述】:

我的 Java 代码有问题:

我想将一个字符串拆分为多个未定义的长度不同的部分。每当字符串中出现空格时,我都想拆分它。

【问题讨论】:

    标签: java


    【解决方案1】:

    您可以像这样轻松拆分字符串:

    class HelloWorld {
        public static void main(String[] args) {
           
            String str = "Hey there welcome to java!";
            String[] splited = str.trim().split("\\s+");
            
            for (String word : splited)
            {
                System.out.println(word); 
            }
        }
    }
    

    输出:

    Hey
    there
    welcome
    to
    java!
    

    【讨论】:

      猜你喜欢
      • 2020-03-09
      • 1970-01-01
      • 2015-10-05
      • 2020-05-26
      • 1970-01-01
      • 2016-01-16
      • 2023-03-11
      • 1970-01-01
      • 2012-11-20
      相关资源
      最近更新 更多