首页javastringJava Data Type - 如何匹配一个字符串的开始和结束

Java Data Type - 如何匹配一个字符串的开始和结束

我们想知道如何匹配一个字符串的开始和结束。

The startsWith() checks if the string starts with the specified argument, whereas endsWith() checks if the string ends with the specified string argument.

Both methods return a boolean value.

public class Main { public static void main(String[] args) { String str = "This is a test"; // Test str, if it starts with "This" if (str.startsWith("This")) { System.out.println("String starts with This"); } else { System.out.println("String does not start with This"); } // Test str, if it ends with "program" if (str.endsWith("program")) { System.out.println("String ends with program"); } else { System.out.println("String does not end with program"); } } }