Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头

In Java

String.startsWith

 boolean     startsWith(String prefix)
          测试此字符串是否以指定的前缀开始。


 boolean     startsWith(String prefix, int toffset)
          测试此字符串是否以指定前缀开始,该前缀以指定索引开始。相当于 this.substring(toffset).startsWith(prefix)

 

StringUtils.startsWith & StringUtils.startsWithIgnoreCase & StringUtils.startsWithAny

org.apache.commons.lang.StringUtils startsWith方法 写道
public static boolean startsWith(String str, String prefix)

Check if a String starts with a specified prefix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case sensitive.

StringUtils.startsWith(null, null) = true
StringUtils.startsWith(null, "abc") = false
StringUtils.startsWith("abcdef", null) = false
StringUtils.startsWith("abcdef", "abc") = true
StringUtils.startsWith("ABCDEF", "abc") = false


Parameters:
str - the String to check, may be null
prefix - the prefix to find, may be null
Returns:
true if the String starts with the prefix, case sensitive, or both null

 

org.apache.commons.lang.StringUtils startsWithIgnoreCase方法 写道
public static boolean startsWithIgnoreCase(String str, String prefix)

Case insensitive check if a String starts with a specified prefix.

nulls are handled without exceptions. Two null references are considered to be equal. The comparison is case insensitive.

StringUtils.startsWithIgnoreCase(null, null) = true
StringUtils.startsWithIgnoreCase(null, "abc") = false
StringUtils.startsWithIgnoreCase("abcdef", null) = false
StringUtils.startsWithIgnoreCase("abcdef", "abc") = true
StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true


Parameters:
str - the String to check, may be null
prefix - the prefix to find, may be null
Returns:
true if the String starts with the prefix, case insensitive, or both null

 

org.apache.commons.lang.StringUtils startsWithAny方法 写道
public static boolean startsWithAny(String string, String[] searchStrings)

Check if a String starts with any of an array of specified strings.

StringUtils.startsWithAny(null, null) = false
StringUtils.startsWithAny(null, new String[] {"abc"}) = false
StringUtils.startsWithAny("abcxyz", null) = false
StringUtils.startsWithAny("abcxyz", new String[] {""}) = false
StringUtils.startsWithAny("abcxyz", new String[] {"abc"}) = true
StringUtils.startsWithAny("abcxyz", new String[] {null, "xyz", "abc"}) = true


Parameters:
string - the String to check, may be null
searchStrings - the Strings to find, may be null or empty
Returns:
true if the String starts with any of the the prefixes, case insensitive, or both null
 

In Bash

使用[[ ]] 模式匹配来判断是否以别的字符串开头(推荐方式)

格式:[[ $STR == $PREFIX* ]]

 

[root@web ~]# STR=ISO9001
[root@web ~]# PREFIX=ISO
[root@web ~]# [[ $STR == $PREFIX* ]] && echo "starts"
starts
[root@web ~]# PREFIX="ISO 9"
[root@web ~]# [[ $STR == $PREFIX* ]] && echo "starts"
[root@web ~]# STR="Yes ISO9001"
[root@web ~]# PREFIX=ISO
[root@web ~]# [[ $STR == $PREFIX* ]] && echo "starts"
[root@web ~]#

 

使用[[ ]] 正则表达式匹配来判断是否以别的字符串开头

格式:[[ $STR =~ ^$PREFIX ]]

注意:必须加上^,否则所有包含该字符串的也会匹配,而不只是以该字符串开头的。

 

[root@web ~]# PREFIX=ISO
[root@web ~]# STR=ISO9001
[root@web ~]# [[ $STR =~ ^$PREFIX ]] && echo "starts"
starts
[root@web ~]# STR="Yes ISO9001"
[root@web ~]# [[ $STR =~ ^$PREFIX ]] && echo "starts"
[root@web ~]#

 

以截取子串判断相等的方式来判断是否以别的字符串开头

格式:N=${#PREFIX}; [ "${STR:0:N}" == "$PREFIX" ]

 

[root@web ~]# PREFIX=ISO
[root@web ~]# STR=ISO9001
[root@web ~]# [ "${STR:0:3}" == "$PREFIX" ] && echo "starts"
starts
[root@web ~]# N=${#PREFIX}
[root@web ~]# [ "${STR:0:N}" == "$PREFIX" ] && echo "starts"
starts
[root@web ~]# [ "${STR:0:$N}" == "$PREFIX" ] && echo "starts"
starts
[root@web ~]#

 

用case语句来判断是否以别的字符串开头

正确:case "$STR" in "$PREFIX"*) echo "starts"; esac

错误:case "$STR" in "$PREFIX*") echo "starts"; esac

注意*不能写在双引号里面,否则不灵。

 

[root@web ~]# PREFIX=ISO
[root@web ~]# STR=ISO9001
[root@web ~]# case "$STR" in "$PREFIX*") echo "starts"; esac
[root@web ~]# case "$STR" in "$PREFIX"*) echo "starts"; esac
starts
[root@web ~]#

 

 

用掐头法判断是否以别的字符串开头

格式:[ "${STR#$PREFIX}" != "$STR" ]

 

[root@web ~]# PREFIX=ISO
[root@web ~]# STR=ISO9001

[root@web ~]# echo "${STR#$PREFIX}"
9001
[root@web ~]# [ "${STR#$PREFIX}" != "$STR" ] && echo "starts with"
starts with
[root@web ~]#

 

 

其他的利用 grep, expr match, expr substr, cut 等的方法,因为都采用外部命令方式,有些杀鸡用牛刀了,此处不列出了。

 

 

本文链接:http://codingstandards.iteye.com/blog/1186542   (转载请注明出处)

返回目录:Java程序员的Bash实用指南系列之字符串处理(目录) 

上节内容:Bash字符串处理(与Java对照) - 15.计算子串出现的次数

下节内容:Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值