.startsWith()
Anonymous contributor
Published Feb 21, 2023
Contribute to Docs
The .startsWith()
method returns true
if a string starts with a given character sequence. Otherwise, false
is returned.
Syntax
string.startsWith(stringOfCharacters);
The stringOfChcaracters
can either be a string literal or a representation of a String
value (variables, constants, etc).
This method will only return case-sensitive matches (i.e. “Word” != “word”).
Example
The following example demonstrates the .startsWith()
method:
import java.io.*;public class Main {public static void main(String[] args) {// With string literalsSystem.out.println("Hello World".startsWith("ello"));// With string objectsString insertBadCoffeePun = "Java";String noMoreJokes = "J";System.out.println("We have had enough of bad coffee puns: " + insertBadCoffeePun.startsWith(noMoreJokes));}}
This will print the following output:
falseWe have had enough of bad coffee puns: true
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Java on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Java
Learn to code in Java — a robust programming language used to create software, web and mobile apps, and more.Beginner Friendly17 hours