find a substring in a string java
Java
package hello;
public class SubStringProblem {
public static void main(String[] args) {
// 1st example - You can use the indexOf() method to check if
// a String contains another substring in Java
// if it does then indexOf() will return the starting index of
// that substring, otherwise it will return -1
System.out
.println("Checking if one String contains another String using indexOf() in Java");
String input = "Java is the best programming language";
boolean isPresent = input.indexOf("Java") != -1 ? true : false;
if (isPresent) {
System.out.println("input string: " + input);
System.out.println("search string: " + "Java");
System.out.println("does String contains substring? " + "YES");
}
// indexOf is case-sensitive so if you pass wrong case, you will get wrong
// result
System.out.println("Doing search with different case");
isPresent = input.indexOf("java") != -1 ? true : false;
System.out.println("isPresent: " + isPresent); // false because indeOf() is
// case-sensitive
// 2nd example - You can also use the contains() method to check if
// a String contains another String in Java or not. This method
// returns a boolean, true if substring is found on String, or false
// otherwise.
// if you need boolean use this method rather than indexOf()
System.out
.println("Checking if one String contains another String using contains() in Java");
input = "C++ is predecessor of Java";
boolean isFound = input.contains("Java");
if (isFound) {
System.out.println("input string: " + input);
System.out.println("search string: " + "Java");
System.out.println("does substring is found inside String? " + "YES");
}
// contains is also case-sensitive
System.out.println("Searching with different case");
isFound = input.contains("java");
System.out.println("isFound: " + isFound); // false because indeOf() is
// case-sensitive
}
}
Output
Checking if one String contains another String using indexOf() in Java
input string: Java is the best programming language
search string: Java
does String contain substring? YES
Doing search with different case
isPresent: false
Checking if one String contains another String using contains() in Java
input string: C++ is the predecessor of Java
search string: Java
does substring is found inside String? YES
Searching for different case
isFound: false
Also in Java:
- Title
- how to find the divisors of a number in java
- Category
- Java
- Title
- find first non repeating character in a string
- Category
- Java
- Title
- java time code
- Category
- Java
- Title
- crit chance in java
- Category
- Java
- Title
- get number of items in arraylist java
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- list in java
- Category
- Java
- Title
- how to push an element in hashset java
- Category
- Java
- Title
- binary search java
- Category
- Java
- Title
- java iterator example
- Category
- Java
- Title
- java loop through object
- Category
- Java
- Title
- how to iterate hashmap in java
- Category
- Java
- Title
- what is a method example in java
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- java script num toSting syntax eror
- Category
- Java
- Title
- java sum of two numbers program intellij
- Category
- Java
- Title
- system.out.println shortcut
- Category
- Java
- Title
- Java int to roman
- Category
- Java
- Title
- how add strings together
- Category
- Java
- Title
- inheritance setter and getter in java
- Category
- Java
- Title
- write an infinite loop java
- Category
- Java
- Title
- java iterate through map
- Category
- Java
- Title
- generate all prime number less than n java (fastest method)
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- linux command to see all the java version installed
- Category
- Java
- Title
- how to break from a loop in java
- Category
- Java
- Title
- math max java
- Category
- Java
- Title
- D/NetworkSecurityConfig: No Network Security Config specified, using platform default
- Category
- Java
- Title
- jtable fill panel
- Category
- Java
- Title
- if else program in java
- Category
- Java
- Title
- jstl core tags
- Category
- Java
- Title
- spring framework iterate
- Category
- Java
- Title
- create object of static class in java
- Category
- Java
- Title
- java mouseevent
- Category
- Java
- Title
- compareto method java
- Category
- Java
- Title
- java test if a string is a int
- Category
- Java
- Title
- code to include layout from java in android
- Category
- Java
- Title
- spigot disable join message
- Category
- Java
- Title
- stream java example
- Category
- Java
- Title
- java how to get current date
- Category
- Java
- Title
- the type javax.servlet.ServletException cannot be resolved. It is indirectly referenced from required .class files
- Category
- Java
- Title
- do while loop java
- Category
- Java
- Title
- dicom read with java
- Category
- Java
- Title
- how to convert a jsonobject to a dbobject
- Category
- Java
- Title
- mutable string in java
- Category
- Java
- Title
- executors java
- Category
- Java
- Title
- array methods in java
- Category
- Java
- Title
- strong password regular expression java
- Category
- Java
- Title
- how to declare an array in java
- Category
- Java
- Title
- rgb to hex java
- Category
- Java
- Title
- java generic calling clone method
- Category
- Java
- Title
- how to copy list item to another list in java
- Category
- Java
- Title
- android cardview dependency
- Category
- Java
- Title
- jbutton set background transparent
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- java 8 hashmap example stackoverflow
- Category
- Java
- Title
- java execute for cycle parallel thread
- Category
- Java
- Title
- how to create dynamic string array in java
- Category
- Java
- Title
- how to create a Scanner Object in java
- Category
- Java
- Title
- double parse jtextfield
- Category
- Java
- Title
- java enum
- Category
- Java
- Title
- random code
- Category
- Java
- Title
- if statement java
- Category
- Java
- Title
- how to iterate hashmap java
- Category
- Java
- Title
- bubble sort in java
- Category
- Java
- Title
- padding a string with 0 in java
- Category
- Java
- Title
- java int passed by reference
- Category
- Java
- Title
- how to change actionbar color in android programmatically
- Category
- Java
- Title
- how to make a list java
- Category
- Java
- Title
- java display message
- Category
- Java
- Title
- java final modifier on method
- Category
- Java
- Title
- java pass arraylist by value
- Category
- Java
- Title
- create java windows application
- Category
- Java
- Title
- remove action bar android studio
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- java create file with content
- Category
- Java
- Title
- multiplication program java
- Category
- Java
- Title
- java convert string with commas to long
- Category
- Java
- Title
- take string from keyboard java
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- how to remove a certain string in a arraylist java
- Category
- Java
- Title
- java fx display simple rectangle
- Category
- Java
- Title
- how to create gravity in java
- Category
- Java
- Title
- searching in database using java
- Category
- Java
- Title
- java repository sql find not in list
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- java switch case
- Category
- Java
- Title
- java iterate through hashmap
- Category
- Java
- Title
- java load image
- Category
- Java
- Title
- get spring application context
- Category
- Java
- Title
- java random numbers in specific range
- Category
- Java
- Title
- processing library cassette
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- how to initialize array in java
- Category
- Java
- Title
- osmdroid offline map does not show
- Category
- Java
- Title
- how to format a datetime object to show date and time and milliseconds in java
- Category
- Java
- Title
- java read from file
- Category
- Java
- Title
- thread sleep java
- Category
- Java
- Title
- how to make a char uppercase in java
- Category
- Java
- Title
- what is java plug-in
- Category
- Java