Java startsWith() Method Guide

In Java, the startsWith() method is used to check if a string begins with a specified prefix. The usage of this method is as follows:

public boolean startsWith(String prefix)

Parameters:

  1. prefix: the string to be checked.

Output:

  1. Return true if the string starts with the specified prefix.
  2. Otherwise, it will return false.

原文:她坦陈了自己的错误。
重述:She admitted her mistake.

String str = "Hello, World!";
System.out.println(str.startsWith("Hello")); // true
System.out.println(str.startsWith("He")); // true
System.out.println(str.startsWith("ello")); // false

In the example above, the string “Hello, World!” starts with “Hello” and “He,” but it does not start with “ello.” Therefore, the startsWith() method returns true, true, and false, respectively.

bannerAds