×

Java String startsWith() method

Java String startsWith() method checks whether current String starts with given prefix or not .

Syntax:

public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int offset)

Parameters:

prefix : It is sequence of character

Returns:

It returns true, if this string starts with given prefix else returns false.

Java String startsWith() method example 1

public class JavaStringStartsWithEx1
{ 
        public static void main(String args[])
       { 
     String s1=("java string split method by tutorialsandexample"); 
     System.out.println(s1.startsWith("ja")); 
     System.out.println(s1.startsWith("java string")); 
       }
}

Output:

true
true

Java String startsWith(String prefix, int offset) Method Example 2

    public class JavaStringStartsWithExample2
    { 
        public static void main(String[] args) { 
            String str = "TutorialAndExample"; 
            System.out.println(str.startsWith("T")); // True 
            System.out.println(str.startsWith("a")); // False 
            System.out.println(str.startsWith("u",1)); // True 
        } 
    }

Output

true
false
true

Java String startsWith() method example 3

 public class JavaStringStartsWithEx3 {
public static void main(String[] args) {
    System.out.println();
    String s1 = "www.tutorialandexamples.com";
    System.out.println(s1);
    String startstr = "www";
    String startstr1 = "http://";
    boolean returnval1 = s1.startsWith(startstr);
    boolean returnval2 = s1.startsWith(startstr1);
    System.out.println(returnval1);
    System.out.println(returnval2);
    System.out.println();
  }
}

Output:

www.tutorialandexamples.com
true
false

Java String startsWith() method example 4

import java.lang.*;
public class JavaStringstartsWithEx4 {
   public static void main(String[] args) {
  System.out.println();
   String str = "www.tutorialandexamples.com";
   System.out.println(str);
   String startstr = "examples";
   // checks that string starts with given substring and starting index
   boolean returnval1 = str.startsWith(startstr);
   // prints true if the string starts with given substring
   System.out.println("starts with " + startstr + " ? " + returnval1);
   }
}

Output:

www.tutorialandexamples.com
starts with examples ? false

Java String startsWith() method example 5

import java.lang.*;
public class JavaStringstartsWithEx5 {
   public static void main(String[] args) {
  System.out.println();
   String str = "www.tutorialandexamples.com";
   System.out.println(str);
   // the start string to be checked
   String startstr = "examples";
   String startstr1 = "examples";
   // checks that string starts with given substring and starting index
   boolean returnval1 = str.startsWith(startstr);
   boolean returnval2 = str.startsWith(startstr1, 16);
   // prints true if the string starts with given substring
   System.out.println("starts with " + startstr + " ? " + returnval1);
   System.out.println("string " + startstr1 + " starting from index 4 ? "
   + returnval2);
   System.out.println();
   }

Output:

www.tutorialandexamples.com
starts with examples ? false
string examples starting from index 4 ? true

Related Topics

Static vs Non-Static in Java

A static method can access and update the value of a static data member. The static keyword is mostly used in Java for memory management. The static keyword can be...

6 minutes read.

How to remove last character from String in Java

In java, there are predominantly three classes connected with the string. The classes are String, StringBuilder, and StringBuffer class that gives techniques connected with string control. Eliminating the first and...

5 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

nth Prime Number in Java

A number is considered prime if only divisible by one and itself. Prime numbers include, for example, 2, 3, 5, 7, 11, etc. In looking at it another way, a...

5 minutes read.

How to Convert int to long in Java

How to Convert int to long in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

Copy data/content from one file to another in java

In this article, you will be acknowledged about how to copy data or content from one file to another file. Also, you will be acknowledged about the classes and methods...

3 minutes read.

Types of Events in Java

One of Java's key ideas is the principle of an event. Events in Java are activities that result in a change in the state or behavior of an object. The...

4 minutes read.

File Operation in Java

In Java, a file is an Abstract data type. These are used to store the data which is related. Files are named storage locations. With a file we can perform...

7 minutes read.

Nth node from the end of the Linked list in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

6 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Java Xmx

This section will explain what Xmx in Java is and how to establish a Java application's maximum heap size. When we execute a Java application, it occasionally displays an error message...

3 minutes read.

Java Math nextDown() Method

The nextDown() method of Math class returns the floating-point number adjacent to the argument in direction of the negative infinity. Syntax: public static double nextDown (double d)public static float nextDown (float f) Parameters: The...

2 minutes read.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Packages Program in java

Let us know what package is in the Java programming language, and later we will learn about types of packages and how to define a package. How to import Java...

4 minutes read.

Ramanujan Number or Taxicab Number in Java

In this section, we will discuss what a Ramanujan number (also known as a Hardy-Ramanujan number) is and how to use a Java programme to determine if a given integer...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.