Answers for "search string in string java"

1

java search string in string

You can use contains(), indexOf() and lastIndexOf() method to check if one String contains another String in Java or not. If a String contains another String then it's known as a substring. The indexOf() method accepts a String and returns the starting position of the string if it exists, otherwise, it will return -1.
Posted by: Guest on September-19-2021
0

find a substring in a string 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
Posted by: Guest on February-07-2020
0

how to check if a string contains a character

public static boolean containsIgnoreCase(String str, char c) {
        str = str.toLowerCase();

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == Character.toLowerCase(c)) {
                return true;
            }
        }
        
        return false;
    }
Posted by: Guest on April-14-2020

Code answers related to "search string in string java"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language