How to check if a string contains a character using indexOf() in Java

1 Answer

0 votes
package javaapplication1;

import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            String s = "java";

            int i = s.indexOf('j');
            System.out.println(i);
            
            i = s.indexOf('a');
            System.out.println(i);
            
            i = s.indexOf('v');
            System.out.println(i);
            
            i = s.indexOf('x');
            System.out.println(i);

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
0
1
2
-1
  
 */

 



answered Nov 21, 2016 by avibootz

Related questions

1 answer 218 views
3 answers 326 views
326 views asked Oct 22, 2016 by avibootz
1 answer 213 views
1 answer 126 views
1 answer 114 views
114 views asked Oct 4, 2023 by avibootz
...