You have given a String and a subString or a character and you need to find out whether given String contains the given character or substring, how would you do that? Well, there are three main ways to search String in Java, the contains() method, the indexOf() method and the lastIndexOf() method, all from java.lang.String class. First method, contains() accepts a CharSequence, superclass of String and return true if that CharSequence appear on the String you have called. The indexOf() and lastIndexOf() are similar method and both return the index (zero based) from where the given subString or character appear in the String you have called. Only difference between indexOf() and lastIndexOf() is that if given substring appear multiple time then they will return difference index, otherwise both will return the same index. Similarly if given sub-string is not present in the String, both will return -1 and contains() will return false.
Here is an example of using contains() to check if String contains a given character in Java:
That's all about how to check if a given substring or a character is present in a String in Java. It's very useful method and often used to solve several String coding questions on programming interviews. Also good knowledge of common String methods like indexOf(), lastIndexOf() and contains() is essential for Java developers and it generally help them to write better code and read code in their day to day activity.
What are 3 ways to check if a given String contains a Character or SubString in Java?
Here are 3 main ways you can use to check if a String contains a given substring or given character in Java. These are using indexOf(), contains() and lastIndexOf() methods of java.lang.String class in Java, also known as God class.
Here is an example:
1. Using indexOf()
This method return the index of given character or substring when you call on a String object. If the given character or substring is not present then it will return -1. Also the index is zero based index. Also, the indexOf() method returns the index of the first occurrence of a specified substring or character in a string. If the substring or character is not found, it returns -1.Here is an example:
public class StringIndexOfExample { public static void main(String[] args) { String text = "Hello, World!"; char characterToCheck = 'W'; // Check if the string contains the given character using indexOf() int indexOfCharacter = text.indexOf(characterToCheck); if (indexOfCharacter != -1) { System.out.println("String contains the character '" + characterToCheck + "' at index " + indexOfCharacter); } else { System.out.println("String does not contain the character '" + characterToCheck + "'"); } } }
2. using contains()
This method return true if found otherwise false. I mean The contains() method checks if a string contains a specified sequence of characters. It returns a boolean value (true if found, false otherwise).Here is an example of using contains() to check if String contains a given character in Java:
public class StringContainsExample { public static void main(String[] args) { String text = "Hello, World!"; String substringToCheck = "World"; // Check if the string contains the given substring using contains() boolean containsSubstring = text.contains(substringToCheck); if (containsSubstring) { System.out.println("String contains the substring '" + substringToCheck + "'"); } else { System.out.println("String does not contain the substring '" + substringToCheck + "'"); } } }
3. Using lastIndexOf()
This method return the last index, i.e. if substring appear multiple times then it will return the second or last index going from left to right.These three examples demonstrate how to use indexOf(), contains(), and lastIndexOf() to check if a string contains a given character or substring in Java. You can choose the appropriate method based on your specific use case and requirements.public class StringLastIndexOfExample { public static void main(String[] args) { String text = "Hello, World!"; char characterToCheck = 'o'; // Check if the string contains the given character using lastIndexOf() int lastIndexOfCharacter = text.lastIndexOf(characterToCheck); if (lastIndexOfCharacter != -1) { System.out.println("String contains the character '" + characterToCheck + "' at index " + lastIndexOfCharacter); } else { System.out.println("String does not contain the character '" + characterToCheck + "'"); } } }
Here is a nice table which summarize the function of these three method with example, you can review this to revise the concept quickly:
That's all about how to check if a given substring or a character is present in a String in Java. It's very useful method and often used to solve several String coding questions on programming interviews. Also good knowledge of common String methods like indexOf(), lastIndexOf() and contains() is essential for Java developers and it generally help them to write better code and read code in their day to day activity.
Other String based coding problems from Interviews:
- How to print all permutations of a String using recursion? (solution)
- How to reverse String in Java without StringBuffer? (solution)
- How to count the number of words in a given String? (solution)
- How to check if a String is a palindrome in Java? (solution)
- How to find duplicate characters on String? (solution)
- How to find one missing number in a sorted array? (solution)
- How to remove an element from an array in Java? (solution)
- How to count vowels and consonants in a given String? (solution)
- Top 30 linked list coding interview questions (see here)
- Top 50 Java Programs from Coding Interviews (see here)
- How to reverse words in a given String in Java? (solution)
- 21 String Programming Questions for Programmers (questions)
- 100+ Data Structure and Algorithms Questions for Java programmers (questions)
- 75+ Programming and Coding Interview questions (questions)
This question was asked to me on coding interview but with a twist, you cannot use the library methods like indexOf, contains, or lastIndexOf(), any idea how to solve in that case?
ReplyDelete