Java Basics:String Methods-4

 

Java String matches()

The Java String matches() method checks whether the string matches the given regular expression or not.

The syntax of the string matches() method is:

string.matches(String regex)

Here, string is an object of the String class.


matches() Parameters

The matches() method takes a single parameter.

  • regex - a regular expression

matches() Return Value

  • returns true if the regex matches the string
  • returns false if the regex doesn't match the string

Example 1: Java matches()

class Main {
  public static void main(String[] args) {

    // a regex pattern for
    // five letter string that starts with 'a' and end with 's'
    String regex = "^a...s$";

    System.out.println("abs".matches(regex)); // false
    System.out.println("alias".matches(regex)); // true
    System.out.println("an abacus".matches(regex)); // false
    System.out.println("abyss".matches(regex)); // true
  }
}

Here, "^a...s$" is a regex, which means 5 letter string that starts with a and ends with s.


Example 2: Check for Numbers

// check whether a string contains only numbers

class Main {
  public static void main(String[] args) {

    // a search pattern for only numbers
    String regex = "^[0-9]+$";

    System.out.println("123a".matches(regex)); // false
    System.out.println("98416".matches(regex)); // true
    System.out.println("98 41".matches(regex)); // false
  }
}

Here, "^[0-9]+$" is a regex, which means only digits.

Java String startsWith()

The Java String startsWith() method checks whether the string begins with the specified string or not.

The syntax of the string startsWith() method is:

string.startsWith(String str, int offset)

Here, string is an object of the String class.


startsWith() Parameters

The startsWith() method can take two parameters.

  • str - check whether string starts with str or not
  • offset (optional) - checks in a substring of string starting from this index.

startsWith() Return Value

  • returns true if the string begins with the given string
  • returns false if the string doesn't begin with the given string

Example 1: Java startsWith() Without Offset Parameter

class Main {
  public static void main(String[] args) {

    String str = "Java Programming";

    System.out.println(str.startsWith("Java")); // true
    System.out.println(str.startsWith("J")); // true
    System.out.println(str.startsWith("Java Program")); // true

    System.out.println(str.startsWith("java")); // false
    System.out.println(str.startsWith("ava")); // false
  }
}

As you can see from the above example, startsWith() takes case (lower case and upper case) into consideration.


Example 2: Java startsWith() With Offset Parameter

class Main {
  public static void main(String[] args) {

    String str = "Java Programming";

    // checks in substring "a Programming"
    System.out.println(str.startsWith("Java", 3)); // false
    System.out.println(str.startsWith("a Pr", 3)); // true
  }
}

Here, we have passed 3 as an offset. Hence, in the above program, startsWith() checks whether "a Programming" begins with the specified string.


Java String endsWith()

The Java String endsWith() method checks whether the string ends with the specified string or not.

The syntax of the string endsWith() method is:

string.endsWith(String str)

Here, string is an object of the String class.


endsWith() Parameters

The endsWith() method takes a single parameter.

  • str - check whether string ends with str or not

endsWith() Return Value

  • returns true if the string ends with the given string
  • returns false if the string doesn't end with the given string

Example: Java endsWith() Without Offset Parameter

class Main {
  public static void main(String[] args) {

    String str = "Java Programming";

    System.out.println(str.endsWith("mming")); // true
    System.out.println(str.endsWith("g")); // true
    System.out.println(str.endsWith("a Programming")); // true

    System.out.println(str.endsWith("programming")); // false
    System.out.println(str.endsWith("Java")); // false
  }
}

As you can see from the above example, endsWith() takes case (lower case and upper case) into consideration.



Comments

Popular posts from this blog

Java OOPS:OOPS Concepts Overview

Java OOPS:Constructors in Java – A complete study!!

Java OOPS: OOPS in Selenium