×

Java Programs

Java Practice

Java program to demonstrate example of String.startsWith() and String.endsWith()?

String.startsWith() and String.endsWith() methods of String class: Here, we will learn how to check whether a string starts with a substring or not and whether a string ends with a substring or not?

1) boolean String.startsWith(String prefix)

This function returns "true" if staring starts with "prefix" (substring) otherwise it returns "false".

2) boolean String.endsWith(String postfix)

This function returns "true" if staring ends with "prefix" (substring) otherwise it returns "false".


Example of String.startsWith()

public class JavaStringStartsWithPrg{
	public static void main(String args[])
	{
		String str="www.includehelp.com";

		if(str.startsWith("www")==true)
			System.out.println("String starts with www");
		else
			System.out.println("String does not start with www");
	}
}

Output

String starts with www

Example of String.endsWith()

public class JavaStringEndsWithPrg{
	public static void main(String args[])
	{
		String str="www.includehelp.com";

		if(str.endsWith(".com")==true)
			System.out.println("String ends with .com");
		else
			System.out.println("String does not end with .com");
	}
}

Output

String ends with .com

Java String Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.