Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

How to split a string in Java

5 Answers

0 votes
public class JavaApplication {
    public static void main(String[] args) {
        
        String s = "123-4567-89";
        String[] parts = s.split("-");
         
        for (String arr : parts) {
            System.out.println(arr);
        }
    }
}
    
    
    
/*
run:
 
123
4567
89
     
*/

 



answered Jan 22, 2016 by avibootz
edited May 2, 2024 by avibootz
0 votes
public class JavaApplication {
    public static void main(String[] args) {
        String s = "abc-defg-hijk";
        String[] arr;
        String delimiter = "-";
        
        arr = s.split(delimiter);

        for (int i = 0; i < arr.length ; i++) {
             System.out.println(arr[i]);
        }
    }
}


   
/*
run:
   
abc
defg
hijk
   
*/

 



answered Oct 26, 2016 by avibootz
edited May 2, 2024 by avibootz
0 votes
public class JavaApplication {
    public static void main(String[] args) {
        String s = "java.php.c++";
        String[] arr;
        String delimiter = "\\.";
        
        arr = s.split(delimiter);

        for (int i = 0; i < arr.length ; i++) {
             System.out.println(arr[i]);
        }
    }
}

   
/*
run:
   
java
php
c++
   
*/

 



answered Oct 26, 2016 by avibootz
edited May 2, 2024 by avibootz
0 votes
public class JavaApplication {
    public static void main(String[] args) {
        String s = "java.php.c++";
        String[] arr;
        String delimiter = "\\.";
        
        // String[] split(String regex, int limit)
        arr = s.split(delimiter, 2);
        
        for (int i =0; i < arr.length ; i++) {
            System.out.println(arr[i]);
        }
    }
}
   
   
   
/*
run:
   
java
php.c++
   
*/

 



answered Oct 26, 2016 by avibootz
edited May 2, 2024 by avibootz
0 votes
public class JavaApplication {
    public static void main(String args[]) {
        String s = "java c c++ python php rust";
 
        String[] arr = s.split(" ");
 
        for (String str : arr) {
            System.out.println(str);
        }
    }
}
 
 
 
 
/*
run:
 
java
c
c++
python
php
rust
 
*/

 



answered May 2, 2024 by avibootz

Related questions

1 answer 91 views
1 answer 76 views
1 answer 112 views
1 answer 102 views
1 answer 99 views
...