stringutils.startwith
时间: 2023-07-30 11:12:02 浏览: 55
`StringUtils.startsWith` 是 Apache Commons Lang 库中提供的一个方法,用于判断一个字符串是否以指定的前缀开始。该方法的定义如下:
```java
public static boolean startsWith(final CharSequence str, final CharSequence prefix) {
return startsWith(str, prefix, false);
}
public static boolean startsWith(final CharSequence str, final CharSequence prefix, final boolean ignoreCase) {
if (str == null || prefix == null) {
return str == null && prefix == null;
}
if (prefix.length() > str.length()) {
return false;
}
return CharSequenceUtils.regionMatches(str, ignoreCase, 0, prefix, 0, prefix.length());
}
```
该方法接收两个参数,第一个参数为需要判断的字符串,第二个参数为需要判断的前缀。方法返回一个布尔值,表示该字符串是否以指定的前缀开始。第三个参数用于指定是否忽略大小写,默认为 false。
例如:
```java
String str = "hello world";
boolean result = StringUtils.startsWith(str, "he");
System.out.println(result); // true
```
上述示例代码中,使用 `StringUtils.startsWith` 方法判断字符串 `str` 是否以 `he` 开头,得到结果为 true。
需要注意的是,该方法判断字符串是否以指定的前缀开始时,会考虑前缀的大小写,如果需要忽略大小写进行判断,可以使用第三个参数指定为 true。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)