JAVA中String分割为数组 String str ="a b c d e f" 期待结果:✀a b✀✀c d✀✀e f✀

也就是以两个以上的空格分隔String str ="a b c d ef"期待结果:a bc def
2024-12-03 02:38:03
推荐回答(3个)
回答1:

import java.util.Arrays;
public class Test{     
    public static void main(String[] args){
        String str ="a b            c d                       ef";
        String a[]=str.split("\\s{2,}");
        System.out.println(Arrays.toString(a));
    }
}
[a b, c d, ef]

回答2:

package com.isoftstone.baidu;
public class DrawDemo {
    public static void main(String[] args) {
        String str = "a b            c d                       ef";
        subStringMethod(str);
    }
       
    public static void subStringMethod(String str) {
        String[] tempValue = str.split("  ");
        for(int i = 0; i < tempValue.length; i++) {
            if(!tempValue[i].trim().equals(""))
                System.out.print(tempValue[i] + "\t");
        }
    }
}

回答3:

substring(0, 3)