How to search String using indexOf in Java

3 Answers

0 votes
public class JavaApplication {
 
    public static void main(String[] args) {
 
        String str = "Java PHP C C# C C++ JavaScript Python";
   
        int index = str.indexOf("C++");
 
        if (index == -1) {
            System.out.println("Not found");
        }
        else {
            System.out.println("Found");
        }
    }
}
  
  
  
/*
run:
  
Found
  
*/

 



answered Oct 22, 2016 by avibootz
edited Jun 1, 2024 by avibootz
0 votes
public class JavaApplication {
 
    public static void main(String[] args) {
 
        String str = "Java PHP C C# C Java C++ JavaScript Python";
   
        int index = str.indexOf("C");
             
        System.out.println(index);
    }
}
  
  
  
/*
run:
  
9
  
*/

 



answered Oct 22, 2016 by avibootz
edited Jun 1, 2024 by avibootz
0 votes
public class JavaApplication {
 
    public static void main(String[] args) {
 
        String str = "Java PHP C C# C Java C++ JavaScript Python";
   
        // public int indexOf(String str, int fromIndex)
        int index = str.indexOf("Java", 3);
             
        System.out.println(index);
    }
}
  
  
  
/*
run:
  
16
  
*/

 



answered Oct 22, 2016 by avibootz
edited Jun 1, 2024 by avibootz

Related questions

1 answer 156 views
2 answers 205 views
1 answer 218 views
1 answer 171 views
1 answer 170 views
1 answer 177 views
...