find a substring in a string java

Solutions on MaxInterview for find a substring in a string java by the best coders in the world

showing results for - "find a substring in a string java"
Azaria
04 Jan 2021
1package hello;
2
3public class SubStringProblem {
4
5  public static void main(String[] args) {
6
7    // 1st example - You can use the indexOf() method to check if
8    // a String contains another substring in Java
9    // if it does then indexOf() will return the starting index of
10    // that substring, otherwise it will return -1
11
12    System.out
13        .println("Checking if one String contains another String using indexOf() in Java");
14    String input = "Java is the best programming language";
15    boolean isPresent = input.indexOf("Java") != -1 ? true : false;
16
17    if (isPresent) {
18      System.out.println("input string: " + input);
19      System.out.println("search string: " + "Java");
20      System.out.println("does String contains substring? " + "YES");
21    }
22
23    // indexOf is case-sensitive so if you pass wrong case, you will get wrong
24    // result
25    System.out.println("Doing search with different case");
26    isPresent = input.indexOf("java") != -1 ? true : false;
27    System.out.println("isPresent: " + isPresent); // false because indeOf() is
28                                                   // case-sensitive
29
30    // 2nd example - You can also use the contains() method to check if
31    // a String contains another String in Java or not. This method
32    // returns a boolean, true if substring is found on String, or false
33    // otherwise.
34    // if you need boolean use this method rather than indexOf()
35    System.out
36        .println("Checking if one String contains another String using contains() in Java");
37    input = "C++ is predecessor of Java";
38    boolean isFound = input.contains("Java");
39    if (isFound) {
40      System.out.println("input string: " + input);
41      System.out.println("search string: " + "Java");
42      System.out.println("does substring is found inside String? " + "YES");
43    }
44
45    // contains is also case-sensitive
46    System.out.println("Searching with different case");
47    isFound = input.contains("java");
48    System.out.println("isFound: " + isFound); // false because indeOf() is
49                                               // case-sensitive
50
51  }
52}
53
54Output
55Checking if one String contains another String using indexOf() in Java
56input string: Java is the best programming language
57search string: Java
58does String contain substring? YES
59Doing search with different case
60isPresent: false
61Checking if one String contains another String using contains() in Java
62input string: C++ is the predecessor of Java
63search string: Java
64does substring is found inside String? YES
65Searching for different case
66isFound: false
queries leading to this page
how to find a particular substring in a string in javahow to find the all the substrings of a string in javacheck that string contain substring using javacheck if substring is present in string javacheck if a substring is in a string javajava find substring using conatinsfind substring in string javafind a word in a string javahow to find substring is in a string in javafind string in string javahow to find substring in a string in javajava find substringstring find substring in javafin substring javahow to find a substrinfg in string in javahow to check if a string is a substring of another javasearch value string javafind substring in a given string javacheck a string javacheak if string contain sub stringcheck if string contains another string javahow to find substrings of a string in javajava check if string contrains another stringjava check if string is substringsearch a substring in javasearch a substring in a string javacheck a substring in a string javahow to find sub string into a string javajava substring presentsubstring in a string matching program in javahow to find if a substring is in a string javahow to check if a string is in another string javajava methods search string for substringfind string in another string javafind substring in java stringif substring is in string javacheck if string contains stringjava test if string in stringjava check is string containtis substring in string javahow to detect substring in a stringfind substring in a string javafind part in string javasearch string in string javacheck for substring in javacheck if string contains substring javacheck character in string javaif string contains substring javafind palindrome substring in string in javajava find location of substring in stringhow to find the substring of a string is a substring of another string in javahow do match substrigns in a string javajava test if string is in other stringhow match sub string in javajava function to check substring in stringsearch for substring in string javaget substring using string from string javacheck a substring in a string in javahow to find substring in string javafinding a substring javafind where substring is javahow to find string value in javahow to find substring in given string javahow to find sub string in javawhich function is used to find the substring of string javahow to check in stringhow to look in a string javahow to check if string has other string ajvasubstring check in javajava substring searchfind substring in java inbuilt functionstring find substring javacheck substring present in string javahow to check string contains substrings in a given order javajava string find substringhow to find a substring is present in a string or not in javasee if string contains substring javahow to find check substring in a string javafinding a substring in a string javajava string search substringhow to check if string contains substringfinding substring in a string javacheck substring in javachecking if a string contains a substring in javastring in another string javasubstring search javahow to search for sub string in a given string in javajava search for a string in another string java check substring in stringseatch a string inside a stringcheck if substring is in a string java without containssearch match any that contain the substrings javajava check if string is contained in another stringsearch for a substring in a string javajava test if string is in another stringfind substring inside string javahow to check if a substring is present in a string in javajava how to find a substring in a stringhow to check if a string contains a substring in javahow to find substring from string in javajava search for substringjava string contains indexofhow to check if a substring is in a string javajava search string for substringhow to search in string in javafind substring of string in javajava find substring in stringhow to find certain word on a string javacheck is substring in string javafinding if it 27s a substring javafind substring function javacheck current string is substring of another string javaprogram to find substring in a string in javafind substring of a string in javajava search string for valuefind a substring in string javafinding substring of a string in javajava find the substringhow to find the substring of a string in javacheck if a string is present in another stringsearching a character in a string in java and return booleanjava search a sub string in stringcheck if a string contains a substring javahow to find substring value in java check for a substring in a string javahow to check if a string has a substring in javajava find a substring in a string find substring in java have given value and find anyof themjava functiun find string from substringhow to find a specific word in a string javasearch for a part of a string in javasearch substring in string javasubstring find sting javahow to find certain substrings javahow to find substring given string in java substring method in javasearching string in a lljava find in stringjava check if string is in stringhow to check if string is in other javafind a specific string inside another string java check for substringfinding string inside string javajava check if substring in stringjava string contains substringjava find character and substrings in stringstring find javahow to match a substring in a string in javafind substring in string in javacheck a string is substring of another javajava find substrings in stringhow to find substring given string in javasearch string for text javastring function to find substring in javahow to check if a string contains a substringsubstring matching in javafinding substrings of string in javawhich method is used to search for a substring 3fhow to find substring in javacheck if substring in string javahow to know if a string contains a substring in javahow to check a substring in a string in javaa string in substring or notjava find string in stringsubstring is present in a string javahow to match substrings javafind substrings in javajava method to find substringhow to check a string is a substring of another in javajava how to check if a char is in a string find out whether this string is a substring in javafind substring in astring javasearching for substrings in strings in javajava string find sub stringjava find word in stringcheck substraing javahow to find where a certain string is inside a string in javasearch contains javacheck if string has 40how to check if text exists in string javahow to find a string in a stringcheck if the value of the string is present in second string or notfind a substring in a string javahow to check if a string contains a substring javafind substr javahow to check if string contains characters in javahow to check if a string is a substring of another in javacheck substring in a string javahow to find string word in javafind substring javasubstring in pythonhow to get substring in string in javafind if given string is substring in javahow to search for a substring in string javafind sub string in string of javasearch a string for a substring javacheck for substring javajava check if string contains stringcheck if a string contains another string javafind string codehow to check if string contains substring in javacheck substring in string javafind substring in javafind all possible substrings of a string javasearch substring in the string in search javasearch substring character javafind a substring in a string java