×

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

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Calculator Program in Java

Calculator Program in Java A calculator performs the basic mathematical operations such as addition, subtraction, multiplication, etc. In this section, we will create a calculator program in Java using switch case...

5 minutes read.

How to check the Java version in cmd

To make programs that can run on our systems, we need to install programming language-related software in our systems. Different programming languages require different types of software, aka IDEs (Integrated...

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.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

How to Read XML Files in Java?

Introduction Today, we are going to learn about how to read XML files in java. Before, reading the XML Files let us learn about XML. XML Files The full form of XML is...

8 minutes read.

Java BufferedWriter

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

Java Strictfp Keyword

Strictfp is used to impose limits on floating-point calculation. It ensures that we will get the same result on every platform while performing an operation with the floating-point variable. The floating-point calculation is platform-dependent due...

1 minute read.

Java String getChars() Method

Java String getChars() method copies characters from current String to the destination character array . Syntax: public void getChars(int srcBeginIndex, int srcEndIndex, char[] destination, int dstBeginIndex) Parameters: srcBegin - index of the first character...

1 minute read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.

Equidigital in Java

In this section, we will understand what is an equidigital number and how to write Java programs to locate them. It is commonly asked in academic settings and Java coding...

4 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute 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.

Java Project Ideas

When it comes to constructing projects, Java is regarded as one of the best languages and is also one of the most paid. Java excels in any application, whether it...

10 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.