Write a note on String comparison and String searching

5.B] Write a note on:
i) String comparison
ii) String searching

Answer:-

String Comparison and Searching in Java

i) String Comparison

String comparison in Java involves checking whether two strings are identical or determining their lexicographical order. Java provides several methods for string comparison:

  1. equals(String anotherString)
    Compares the content of two strings. It returns true if both strings have the same characters in the same order.
    Example:
   String str1 = "Hello";
   String str2 = "Hello";
   boolean isEqual = str1.equals(str2);
   System.out.println("Strings are equal: " + isEqual); // Output: Strings are equal: true
  1. equalsIgnoreCase(String anotherString)
    Compares two strings while ignoring case differences.
    Example:
   String str1 = "Hello";
   String str2 = "hello";
   boolean isEqualIgnoreCase = str1.equalsIgnoreCase(str2);
   System.out.println("Strings are equal (ignoring case): " + isEqualIgnoreCase); // Output: Strings are equal (ignoring case): true
  1. compareTo(String anotherString)
    Compares two strings lexicographically. It returns a negative integer if the first string is lexicographically less than the second, zero if they are equal, and a positive integer if the first string is greater.
    Example:
   String str1 = "Apple";
   String str2 = "Banana";
   int result = str1.compareTo(str2);
   System.out.println("Comparison result: " + result); // Output: Comparison result: -1
  1. compareToIgnoreCase(String anotherString)
    Similar to compareTo(), but ignores case differences during comparison.
    Example:
   String str1 = "apple";
   String str2 = "Banana";
   int result = str1.compareToIgnoreCase(str2);
   System.out.println("Comparison result (ignoring case): " + result); // Output: Comparison result (ignoring case): -1

ii) String Searching

String searching involves finding the occurrence or position of a substring within a string. Java provides various methods for searching within strings:

  1. contains(CharSequence sequence)
    Checks if a string contains a specified sequence of characters.
    Example:
   String str = "Hello, World!";
   boolean containsHello = str.contains("Hello");
   System.out.println("Contains 'Hello': " + containsHello); // Output: Contains 'Hello': true
  1. indexOf(String str)
    Returns the index of the first occurrence of the specified substring. Returns -1 if the substring is not found.
    Example:
   String str = "Hello, World!";
   int index = str.indexOf("World");
   System.out.println("Index of 'World': " + index); // Output: Index of 'World': 7
  1. lastIndexOf(String str)
    Returns the index of the last occurrence of the specified substring.
    Example:
   String str = "Hello, World! Hello, Java!";
   int lastIndex = str.lastIndexOf("Hello");
   System.out.println("Last index of 'Hello': " + lastIndex); // Output: Last index of 'Hello': 14
  1. startsWith(String prefix)
    Checks if a string starts with the specified prefix.
    Example:
   String str = "Hello, World!";
   boolean startsWithHello = str.startsWith("Hello");
   System.out.println("Starts with 'Hello': " + startsWithHello); // Output: Starts with 'Hello': true
  1. endsWith(String suffix)
    Checks if a string ends with the specified suffix.
    Example:
   String str = "Hello, World!";
   boolean endsWithWorld = str.endsWith("World!");
   System.out.println("Ends with 'World!': " + endsWithWorld); // Output: Ends with 'World!': true
  1. matches(String regex)
    Checks if the string matches the specified regular expression.
    Example:
   String str = "12345";
   boolean isNumeric = str.matches("\\d+"); // \\d+ is a regular expression for one or more digits
   System.out.println("Is numeric: " + isNumeric); // Output: Is numeric: true

Leave a Reply

Your email address will not be published. Required fields are marked *