Simple string search in Java

public class StringSearch { public static void main(String[] args) { String myString = "I am a String!"; if(myString.indexOf("String") == -1) { System.out.println("String not Found!"); } else { System.out.println("String found at: " + myString.indexOf("String")); } } }
Java offers a Library method called indexOf().

This method is used with String Object and it returns the position of index of desired string. If the string is not found then -1 is returned.

#java #string #search

#cesarnog

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.