Java String startsWith()

The startsWith() method tests if the substring of this string beginning at the specified index starts with the specified prefix.

Java Platform:

Java SE 8

Syntax:
startsWith(String prefix,int toffset) or public boolean startsWith(String prefix) public boolean startsWith(String prefix, int offset)
Internal implementation
public boolean startsWith(String prefix, int toffset) { char ta[] = value; int to = toffset; char pa[] = prefix.value; int po = 0; int pc = prefix.value.length; // Note: toffset might be near -1>>>1. if ((toffset < 0) || (toffset > value.length - pc)) { return false; } while (--pc >= 0) { if (ta[to++] != pa[po++]) { return false; } } return true; }
Parameter

prefix : Sequence of character

Returns

true or false

Return Value: true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset; false otherwise. The result is false if toffset is negative or greater than the length of this String object; otherwise the result is the same as the result of the expression.

this.substring(toffset).startsWith(prefix)

Return Value Type: boolean

Example: Java String startsWith(String prefix, int toffset) Method
import java.lang.*; public class Test { public static void main(String[] args) { System.out.println(); String str = "www.cprogramcoding.com"; System.out.println(str); String startstr = "cprogramcoding"; String startstr1 = "cprogramcoding"; boolean returnval1 = str.startsWith(startstr); boolean returnval2 = str.startsWith(startstr1, 4); System.out.println("starts with " + startstr + " ? " + returnval1); System.out.println("string " + startstr1 + " starting from index 4 ? " + returnval2); System.out.println(); } }


Output:
www.cprogramcoding.com starts with cprogramcoding ? false string cprogramcoding starting from index 4 ? true
public boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
Java Platform: Java SE 8
Syntax:
startsWith(String prefix)

Return Value: true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.

Return Value Type: boolean

Example: Java String startsWith(String prefix) Method

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


Output:
www.cprogramcoding.com true false
Java String startsWith() method example
public class Test { public static void main(String args[]) { String str1="java string split method by cprogramcoding"; System.out.println(str1.startsWith("ja")); System.out.println(str1.startsWith("java string")); } }


Output:
true true
Java String startsWith(String prefix, int offset) Method Example

This is overloaded method of startWith() method which is used to pass one extra argument (offset) to the function. This method works from the passed offset.

public class Test { public static void main(String[] args) { String str = "cprogramcoding"; System.out.println(str.startsWith("c")); System.out.println(str.startsWith("p")); System.out.println(str.startsWith("a",1)); } }


Output:
true false false



Instagram