🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this guide, you will learn about the String startsWith() method in Java programming and how to use it with an example.
1. String startsWith() Method Overview
Definition:
The startsWith() method of Java's String class checks if the string starts with the specified prefix. It returns a boolean value indicating the result.
Syntax:
1. str.startsWith(String prefix)
2. str.startsWith(String prefix, int offset)
Parameters:
- prefix: the substring to be matched.
- offset: the index from where to begin looking for the prefix in the string.
Key Points:
- The method is case-sensitive, meaning "JAVA" and "java" are treated as different prefixes.
- When the offset parameter is used, the method checks if the substring of this string beginning at the specified index starts with the specified prefix.
2. String startsWith() Method Example
public class StartsWithExample {
public static void main(String[] args) {
String sample = "JavaProgrammingIsFun";
// Checking if the string starts with "Java"
boolean startsWithJava = sample.startsWith("Java");
System.out.println("String starts with 'Java'? " + startsWithJava);
// Checking if the string starts with "JAVA"
boolean startsWithJAVA = sample.startsWith("JAVA");
System.out.println("String starts with 'JAVA'? " + startsWithJAVA);
// Using offset to check if the string from index 4 starts with "Programming"
boolean startsWithProgramming = sample.startsWith("Programming", 4);
System.out.println("Substring from index 4 starts with 'Programming'? " + startsWithProgramming);
}
}
Output:
String starts with 'Java'? true String starts with 'JAVA'? false Substring from index 4 starts with 'Programming'? true
Explanation:
In the example:
1. We check if the string starts with the prefix "Java". The method returns true because the string indeed begins with "Java".
2. We then check if the string starts with the prefix "JAVA". The method returns false because it is case-sensitive, and "JAVA" is not the same as "Java".
3. Finally, we use the offset parameter to check if the substring starting from index 4 (0-based) of our string starts with "Programming". The method returns true as "Programming" is the substring starting from the 4th index of the string.
Related Java String Class method examples
- Java String charAt() example
- Java String concat() example
- Java String contains() example
- Java String endsWith() example
- Java String equals() example
- Java String equalsIgnoreCase() example
- Java String getBytes() example
- Java String indexOf() example
- Java String isEmpty() example
- Java String lastIndexOf() example
- Java String length() example
- Java String replace() example
- Java String split() example
- Java String startsWith() example
- Java String substring() example
- Java String toLowerCase() example
- Java String toUpperCase() example
- Java String trim() example
- Java String valueOf() example
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment