➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
StringUtils startsWith() Example | In this blog, we will discuss the startsWith() method of StringUtils class of the Apache Common Lang package. This method returns the boolean value that is if the string starts with the given character or character sequence then it returns true, or else if it does not start with the given value then it returns false.
Basically, the startsWith method is a string-handling method that helps to check whether the given string starts with a particular character/string or not. This method is case sensitive and if there is a small letter at the beginning of the string and you search for capital letters, then this method returns false.
The syntax of StringUtils startsWith() Method
The syntax of StringUtils startsWith() Method is as follows:-
public static boolean startsWith(final CharSequence str, final CharSequence prefix)
Check if a CharSequence starts with a specified prefix.
Parameters:-
str: the given string which needs to be checked.
prefix: the first letter or first substring which says whether the string starts with or not.
Return type: boolean
Returns: true if the string has given the start character as the first letter or false if it does not have the given character as the first letter.
Nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case-sensitive.
Example of the StringUtils.startsWith() Method
Example-1:
String: “Apples”
Starts With: “A”
True
Example-2:
String: “Apples”
Starts with: “a”
False
Java Program For StringUtils startsWith() Method
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String str = "Hello";
System.out.println(StringUtils.startsWith(str, "H"));
System.out.println(StringUtils.startsWith(str, "h"));
System.out.println(StringUtils.startsWith(str, "Hel"));
System.out.println(StringUtils.startsWith(str, "World"));
System.out.println(StringUtils.startsWith(str, "Hello"));
}
}
Output:-
true
false
true
false
true
Since the startsWith() method compare case wise therefore StringUtils.startsWith(str, “h”) returns false but StringUtils.startsWith(str, “H”) returns true.
Nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case-sensitive.
import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
String str = null;
System.out.println(StringUtils.startsWith(str, null));
System.out.println(StringUtils.startsWith(str, "n"));
System.out.println(StringUtils.startsWith(str, "null"));
System.out.println(StringUtils.startsWith(str, "NULL"));
}
}
Output:-
true
false
false
false
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!