Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String startsWith() method
Syntax and Examples



Introduction

The startsWith() method in Java is a handy tool for checking if a string begins with a specified prefix. It's like verifying if a book title starts with a certain word, or confirming that a file name follows a particular pattern.

Syntax

    public boolean startsWith(String prefix)
    public boolean startsWith(String prefix, int toffset)
  

Parameters

Here's a breakdown of the parameters accepted by startsWith():

Parameter Description
prefix The string to check if the original string starts with.
prefix The string to check if the original string starts with.
toffset An offset from which to start checking the prefix. This allows you to skip a certain number of characters at the beginning of the string before comparing. Must be non-negative and less than or equal to the length of this string.

Return Value

The startsWith() method returns a boolean value:

  • true: If the string starts with the specified prefix.
  • false: Otherwise.

Examples

Example 1: Simple Prefix Check

This example demonstrates how to use startsWith() with a simple prefix.

    String str = "Hello World";
    String prefix = "Hello";

    boolean startsWithPrefix = str.startsWith(prefix);

    System.out.println(startsWithPrefix); // Output: true
  
    true
  

In this case, we check if the string str starts with the prefix Hello. Because it does, the method returns true.

Example 2: Case Sensitivity

This example highlights that startsWith() is case-sensitive.

    String str = "Hello World";
    String prefix = "hello"; // Note the lowercase 'h'

    boolean startsWithPrefix = str.startsWith(prefix);

    System.out.println(startsWithPrefix);
  
    false
  

Because the first character of str is 'H' (uppercase), while the prefix starts with 'h' (lowercase), the method returns false. Case matters!

Example 3: Using the toffset Parameter

This example shows how to use the toffset parameter to ignore a certain number of characters at the beginning of the string.

    String str = "  Hello World"; // String with leading spaces
    String prefix = "Hello";

    boolean startsWithPrefix = str.startsWith(prefix, 2); // Check starting from index 2 (skipping the first two characters)

    System.out.println(startsWithPrefix);
  
    true
  

Here, we're checking if the string str starts with Hello, but we're starting our check from index 2. This effectively ignores the two leading spaces. Because the substring starting at index 2 is indeed "Hello", the method returns true.

Example 4: Empty Prefix

This example demonstrates what happens when you use an empty string as a prefix

        String str = "Hello World";
        String prefix = ""; // An empty String
        boolean startsWithPrefix = str.startsWith(prefix);
        System.out.println(startsWithPrefix);
    
        true
    

An empty string is considered a prefix of any string, so the method returns true.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M