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
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
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
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实用指南系列之字符串处理(目录)