Java String startsWith() Method
Why we use strings in Java?
Strings are used in Java (and in many other programming languages) to represent text. In Java, a string is a sequence of characters stored in a char array and is represented by the String class.
- Strings are commonly used to store and display text such as names, addresses, and other kinds of information. They are also used to store and manipulate data read from external sources, such as text files or user input.
To know more about String Class in java read the complete article.
What is the use of startswith method in Java?
The startsWith() function of the Java String class determines whether this string begins with the specified prefix. If this text begins with the specified prefix, it returns true; otherwise, it returns false.
The String.prototype.startsWith() method determines whether a string begins with the characters of a specified string. This method returns a boolean value indicating whether the original string starts with the characters of the specified string.
The java.lang.String class provides a lot of built-in methods that are used to manipulate string in Java.These methods help us to perform operations on String objects such as trimming, concatenating, converting, comparing, replacing strings etc.
- If you need to perform a case-insensitive search, you can use the toLowerCase or toUpperCase method to convert both the original string and the search string to the same case before calling startsWith.
Syntax:
public boolean startsWith(String prefix)
Parameters:
prefix - the prefix.
Let’s look at a string-related Java program where the Java String startsWith() Method is used to perform an operation on the given string.
Example: Java String startsWith Method
public class Main { public static void main(String[] args) { String myStr = "Hello PrepInsta "; System.out.println(myStr.startsWith("Hel")); System.out.println(myStr.startsWith("Pre")); System.out.println(myStr.startsWith("I")); } }
Output
true false false
Example 2 : Java Spilt Method
public class Main { public static void main(String[] args) { String s1="java string Method by PrepInsta and PRIME"; System.out.println(s1.startsWith("ja")); System.out.println(s1.startsWith("java string")); System.out.println(s1.startsWith("Java string")); } }
Output
true true false
Example 3: Java Spilt Method Using Regex and Length
public class Main { public static void main(String[] args) { String s1="PrepInsta is best for coding preparation "; System.out.println(s1.startsWith("Prep")); System.out.println(s1.startsWith("prepInsta")); System.out.println(s1.startsWith("PrepInsta")); } }
Output
true false true
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment