How to split a string at the last occurrence of a separator in Java

1 Answer

0 votes
public class Program {
    public static void main(String[] args) {
        String str = "java go c rust c++ python";

        String part1 = str.substring(0, str.lastIndexOf('c'));
        String part2 = str.substring(str.lastIndexOf('c'));
 
        System.out.println(part1); 
        System.out.println(part2); 
    }
}
 
 
 
/*
run:
 
java go c rust 
c++ python
 
*/

 



answered May 24, 2024 by avibootz

Related questions

...