String

String startsWith method

In this example we shall show you how to use the startsWith method of String class. The String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. To check if a String starts with a specified prefix one should perform the following steps:

  • Create a new String.
  • Use startsWith(String prefix) API method of String. It tests if this string starts with the specified prefix,

as described in the code snippet below.
 

package com.javacodegeeks.snippets.core;

public class StringStartsWithMethod {
	
	public static void main(String[] args) {
		
		String s = "Java Code Geeks";
		
		if (s.startsWith("Java")) {
			System.out.println("String starts with Java");
		}
		else {
			System.out.println("String does not start with Java");
		}
		
		if (s.startsWith("Unknown")) {
			System.out.println("String starts with Unknown");
		}
		else {
			System.out.println("String does not start with Unknown");
		}
		
	}

}

Output:

String starts with Java
String does not start with Unknown

 
This was an example of startsWith method of String in Java.

Byron Kiourtzoglou

Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button