java tutorial - Java Strings startsWith() method - java programming - learn java - java basics - java for beginners



Java strings startswith

Learn Java - Java tutorial - Java strings startswith - Java examples - Java programs

Description

The method startsWith () has two options and checks whether the string starts with the specified prefix, starting at the specified index or from the beginning (by default).

Syntax

Syntax method:

public boolean startsWith(String prefix, int toffset)

or

public boolean startsWith(String prefix)
click below button to copy the code. By - java tutorial - team

Options

Detailed information about the parameters:

  • prefix — the prefix to be matched;
  • toffset is the initial search index in the string.

Return value

  • In Java, startsWith () method returns true if the sequence of characters represented by the argument is the prefix of the sequence of characters represented by the given string; or else false.

Sample Code

import java.io.*;
public class Test {

public static void main(String args[]) {
String Str = new String("Welcome to wikitechy.com");

System.out.print("Return value:");
System.out.println (Str.startsWith ("Welcome"));

System.out.print("Return value:");
System.out.println (Str.startsWith ("ProgLang"));

System.out.print("Return value:");
System.out.println (Str.startsWith ("ProgLang", 20));
}
  
}
click below button to copy the code. By - java tutorial - team

Output

Return value:true
Return value:false
Return value:false

Related Searches to Java Strings startsWith() method