×

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

Chromatic Number in Java

The chromatic number is the bare minimum of colours necessary to accurately colour any graph. To put it another way, the chromatic number can be thought of as the bare...

6 minutes read.

Program to Find Square Root of a Number Without sqrt Method in Java

The Java Math class sqrt () function can be used to determine the square root of a number. In this section, we'll write a Java program to find a number's...

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.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Web Crawler in Java

In this article, you will be acknowledged with what a web crawler in java is and what are its functions. You will also be able to understand where to implement...

4 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Java Programming certification

The most common technology utilized in the creation of applications is Java. People and businesses like it because it turns original ideas into useful software solutions. A Java programming certification can...

6 minutes read.

Annotations in Java

Annotations in Java Java Annotations are metadata about the source code. They do not have any direct effect on the execution of the java program. Annotations in Java were introduced in...

4 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java CountDownLatch

Another crucial classes for concurrent execution is CountDownLatch. It is a synchronisation tool that enables one or more threads to await until a series of tasks started by another thread...

4 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Java String trim() method

Java String trim() method returns String after eliminating leading and trailing spaces. Note:- Java String trim() method doesn't trim or omit middle spaces. Syntax: public String trim() Returns: It returns string with omitted leading and...

2 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Find Unique Elements in Array Java

An array in Java is a grouping of objects that share the same type of data. We are free to enter identical or repeating elements into an array. Therefore, we...

6 minutes read.

Hashtable in Java

Hashtable in Java The Hashtable class implements the Map interface and extends the Dictionary class. It implements a hash table which shows the key-value relation, i.e., it maps the keys to the values....

9 minutes read.