Java String Startstwith示例

时间:2020-02-23 14:35:14  来源:igfitidea点击:

String的startswith方法检查字符串是否以特定的前缀开头。
它返回布尔数据类型。

方法签名:

public boolean startsWith(String prefix)

String startstwith示例:

package org.igi.theitroad;
 
public class StringStartsWithExample {
 
 public static void main(String[] args) {
  String str = "theitroad";
 
  if (str.startsWith("java")) {
   System.out.println("String starts with java");
  } 
  else {
   System.out.println("String does not start with java");
  }
 
  if (str.startsWith("JAVA")) {
   System.out.println("String starts with JAVA");
  } 
  else {
   System.out.println("String does not start with JAVA");
  }
 }
 
}

运行上面的程序时,我们将得到以下输出:

String starts with java
String does not start with JAVA

请注意startsWith方法是区分大小写的,如上面的示例所示