How to get substring from a string till specific character in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String str = "java c c++ python c#";
        char ch = 'y';

        int index = str.indexOf(ch);
        
        if (index != -1) {
            String sub = str.substring(0, index);
            System.out.println(sub);  
        }
    }
}



/*
run:
     
java c c++ p
     
*/

 



answered Oct 6, 2023 by avibootz

Related questions

...