How to find the start and end position of a word in a string in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        String s = "Java programming language, java object-oriented, java class-base";
        String word = "programming";

        int st = s.indexOf(word);
        int en = st + word.length();
        
        System.out.println(st + " " + en);
    }
}



/*
run:

49
27
0

*/

 



answered Apr 27, 2019 by avibootz
...