The startsWith()
method is a built-in method in Java’s String class that is used to check if a string starts with a specified prefix.
Syntax:
public boolean startsWith(String prefix)
The startsWith()
method takes one argument, which is the prefix to be checked. It returns a boolean value, which is true
if the string starts with the specified prefix, otherwise, it returns false
.
Example:
String str = "Hello World"; boolean result = str.startsWith("Hello"); if(result) { System.out.println("The string starts with the specified prefix"); } else { System.out.println("The string does not start with the specified prefix"); }
Output:
The string starts with the specified prefix
In the above example, the startsWith()
method is used to check whether the string str
starts with the prefix “Hello”. Since the string starts with “Hello”, the startsWith()
method returns true
.
Signature of Java String startsWith():
The signature of the startsWith()
method in Java’s String class is:
public boolean startsWith(String prefix)
Here, the method name is startsWith()
, which takes one argument of type String
named prefix
. The method returns a boolean value true
if the string starts with the specified prefix, otherwise, it returns false
.
Parameter of Java String startsWith():
The startsWith()
method in Java’s String class takes one parameter, which is of type String
.
The parameter is named prefix
, and it specifies the prefix to be checked in the given string. The startsWith()
method checks whether the given string starts with the specified prefix or not.
Syntax:
public boolean startsWith(String prefix)
Example:
String str = "Hello World"; boolean result = str.startsWith("Hello"); if(result) { System.out.println("The string starts with the specified prefix"); } else { System.out.println("The string does not start with the specified prefix"); }
Output:
The string starts with the specified prefix
In the above example, the parameter of the startsWith()
method is the String
object "Hello"
, which is the prefix to be checked in the given string.
The startsWith()
method in Java’s String class returns a boolean value. It returns true
if the string starts with the specified prefix, otherwise, it returns false
.
Syntax:
public boolean startsWith(String prefix)
Example:
String str = "Hello World"; boolean result = str.startsWith("Hello"); if(result) { System.out.println("The string starts with the specified prefix"); } else { System.out.println("The string does not start with the specified prefix"); }
Output:
The string starts with the specified prefix
In the above example, the startsWith()
method returns true
, as the given string str
starts with the specified prefix "Hello"
.
Internal implementation of startsWith(String prefix, int toffset):
The startsWith(String prefix, int toffset)
method in Java’s String class is used to check whether a substring of the given string starting from the specified toffset
index starts with the specified prefix or not.
The method has two parameters:
prefix
: The prefix to be checked.toffset
: The index from which the substring is to be checked.
The method returns a boolean value true
if the substring starting from the specified toffset
index starts with the specified prefix, otherwise, it returns false
.
The internal implementation of the startsWith(String prefix, int toffset)
method can be described as follows:
- Check if the specified
toffset
index is negative or greater than the length of the string. If it is, throw anIndexOutOfBoundsException
. - Calculate the length of the prefix.
- If the length of the prefix is greater than the remaining length of the string from the specified
toffset
index, returnfalse
. - Iterate over the characters of the prefix and the corresponding characters in the string starting from the specified
toffset
index. - If any corresponding character in the prefix and the string do not match, return
false
. - If all characters match, return
true
.
Here is an example usage of the startsWith(String prefix, int toffset)
method:
String str = "Hello World"; boolean result = str.startsWith("World", 6); if (result) { System.out.println("The substring starting from index 6 starts with the prefix 'World'"); } else { System.out.println("The substring starting from index 6 does not start with the prefix 'World'"); }
Output:
The substring starting from index 6 starts with the prefix 'World'
In the above example, the startsWith(String prefix, int toffset)
method is used to check whether the substring starting from index 6 of the given string "Hello World"
starts with the prefix "World"
. The method returns true
, as the substring starting from index 6 of the given string is "World"
.
Internal Implementation of startsWith(String prefix,):
The startsWith(String prefix)
method in Java’s String class is used to check whether the given string starts with the specified prefix or not.
The method has one parameter:
prefix
: The prefix to be checked.
The method returns a boolean value true
if the string starts with the specified prefix, otherwise, it returns false
.
The internal implementation of the startsWith(String prefix)
method can be described as follows:
- Calculate the length of the prefix.
- If the length of the prefix is greater than the length of the string, return
false
. - Iterate over the characters of the prefix and the corresponding characters in the string.
- If any corresponding character in the prefix and the string do not match, return
false
. - If all characters match, return
true
.
Here is an example usage of the startsWith(String prefix)
method:
String str = "Hello World"; boolean result = str.startsWith("Hello"); if (result) { System.out.println("The string starts with the prefix 'Hello'"); } else { System.out.println("The string does not start with the prefix 'Hello'"); }
Output:
The string starts with the prefix 'Hello'
In the above example, the startsWith(String prefix)
method is used to check whether the given string "Hello World"
starts with the prefix "Hello"
. The method returns true
, as the given string starts with "Hello"
.
Java String startsWith() method example:
Here’s an example usage of the startsWith()
method in Java’s String class:
String str1 = "Hello World"; String str2 = "Hello"; boolean result = str1.startsWith(str2); if(result) { System.out.println("The string starts with the specified prefix"); } else { System.out.println("The string does not start with the specified prefix"); }
Output:
The string starts with the specified prefix
In the above example, the startsWith()
method is used to check whether the string str1
starts with the prefix str2
. The method returns true
, as the string str1
starts with str2
.
Java String startsWith(String prefix, int offset) Method Example:
Here’s an example usage of the startsWith(String prefix, int offset)
method in Java’s String class:
String str = "Hello World"; boolean result = str.startsWith("World", 6); if (result) { System.out.println("The substring starting from index 6 starts with the prefix 'World'"); } else { System.out.println("The substring starting from index 6 does not start with the prefix 'World'"); }
Output:
The substring starting from index 6 starts with the prefix 'World'
In the above example, the startsWith(String prefix, int offset)
method is used to check whether the substring starting from index 6 of the given string "Hello World"
starts with the prefix "World"
. The method returns true
, as the substring starting from index 6 of the given string is "World"
.