How to use regular expression to match any amount of whitespace in Java

2 Answers

0 votes
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) throws Exception {  
        String str = "java     c         c++   python";
        
        String[] words = str.split("\\s+");

        System.out.println(Arrays.toString(words));
    }
}



/*
run:
 
[java, c, c++, python]
 
*/

 



answered Oct 11, 2023 by avibootz
0 votes
public class MyClass {
    public static void main(String args[]) throws Exception {  
        String str = "java     c         c++   python";
        
        str = str.replaceAll("\\s+", " ");

        System.out.println(str);
    }
}



/*
run:
 
java c c++ python
 
*/

 



answered Oct 11, 2023 by avibootz
...