How to split a string using a given delimiter in Java

2 Answers

0 votes
import java.util.Arrays;
 
public class MyClass
{
    public static void main(String[] args)
    {
        String str = "java-c-c++-rust-python";
        
        String[] result = str.split("-");
 
        System.out.println(Arrays.toString(result));
    }
}




/*
run:

[java, c, c++, rust, python]

*/

 



answered Mar 19, 2023 by avibootz
0 votes
import java.util.StringTokenizer;
 
public class MyClass
{
    public static void main(String[] args)
    {
        String str = "java-c-c++-rust-python";
        String delimiter = "-";
        
        StringTokenizer result = new StringTokenizer(str, delimiter);
        
        while (result.hasMoreTokens()) {
            System.out.println(result.nextToken());
        }
    }
}




/*
run:

java
c
c++
rust
python

*/

 



answered Mar 19, 2023 by avibootz

Related questions

1 answer 110 views
1 answer 120 views
1 answer 143 views
1 answer 193 views
1 answer 107 views
...