String类的一些方法

时间:2023-03-09 03:14:01
String类的一些方法

  String 类有以下方法:

startsWith(String prefix)

boolean java.lang.String.startsWith(String prefix)

Tests if this string starts with the specified prefix.

Parameters:
prefix the prefix.
Returns:
true if the character sequence represented by the argument is a prefix of the character sequence represented by this string; false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.
Since:
1. 0
就是判断字符串中的后缀是否为参数prefix,如果是就返回true,如果不是就返回false。
这在安卓中可以用来识别音频文件,如mp3文件等。
对应的,还有一个方法,
endsWith(String suffix)
boolean java.lang.String.endsWith(String suffix)

Tests if this string ends with the specified suffix.

Parameters:
suffix the suffix.
Returns:
true if the character sequence represented by the argument is a
suffix of the character sequence represented by this object; false
otherwise. Note that the result will be true if the argument is the
empty string or is equal to this String object as determined by the
equals(Object)
method.
可以用来判断一个字符串是否以suffix这个参数开头。