×

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

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

Java Macros

Macros are additions to the JDK 7 compiler in Java. Compile time macros are added and supported. Macros are Java classes that are instantiated and executed during the compilation process....

2 minutes read.

Java Math toRadians() Method

The toRadians() method of Java Math class converts an angle measured in degrees to an approximately equivalent angle measured in radian. Syntax: public static double toRadians (double angdeg) Parameters The parameter ‘angdeg’ represents an...

2 minutes read.

Bubble Sort in Java

Bubble Sort in Java Bubble sort isalso known as sinking sort. It is one of the simplest sorting algorithms. In the bubble sort algorithm, the given array is traversed from left...

5 minutes read.

Java Double Keyword

In java primitive data types, we have two different types of data types which are Boolean and floating-point data types.In floating data type again we have four types which are...

3 minutes read.

Java Worker Class

What is a Worker? A service which executes Work - flow and Activities is referred to as a Worker. On user-controlled hosts, workers are defined and put into action. The Worker...

3 minutes read.

What are Array strings in Java?

In normal programming, An array is a group and a collection of identical forms of data that are stored in a sequential memory region and may be accessed using their...

4 minutes read.

How to Convert String to char in Java

How to Convert String to char in Java There are two methods to convert String to char are: Using charAt() method Using tocharArray() method Using charAt() method This is the method of String class that...

3 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Java Session

Session indicates interval of time. A session is a simple  time interval in which servers and client interacts. To maintain the state of the client or user we use technologies...

5 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

Example of Static import in Java

Static keyword Java's static keyword is mainly used to control memory. Variables, methods, blocks, and nested classes are compatible with the static keyword. Instead of an instance, the static keyword is...

3 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Palindrome Partitioning problem in Java

In this article, you will be acknowledged about partitioning of the palindrome. It can be done in many ways. This article makes sure every method is discussed. Palindrome If a string remains...

6 minutes read.

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.