天天看点

Bash字符串处理(与Java对照) - 16.判断是否以另外的字符串开头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* ]]

[[email protected] ~]# STR=ISO9001

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# [[ $STR == $PREFIX* ]] && echo "starts"

starts

[[email protected] ~]# PREFIX="ISO 9"

[[email protected] ~]# [[ $STR == $PREFIX* ]] && echo "starts"

[[email protected] ~]# STR="Yes ISO9001"

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# [[ $STR == $PREFIX* ]] && echo "starts"

[[email protected] ~]#

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

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

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

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# STR=ISO9001

[[email protected] ~]# [[ $STR =~ ^$PREFIX ]] && echo "starts"

starts

[[email protected] ~]# STR="Yes ISO9001"

[[email protected] ~]# [[ $STR =~ ^$PREFIX ]] && echo "starts"

[[email protected] ~]#

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

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

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# STR=ISO9001

[[email protected] ~]# [ "${STR:0:3}" == "$PREFIX" ] && echo "starts"

starts

[[email protected] ~]# N=${#PREFIX}

[[email protected] ~]# [ "${STR:0:N}" == "$PREFIX" ] && echo "starts"

starts

[[email protected] ~]# [ "${STR:0:$N}" == "$PREFIX" ] && echo "starts"

starts

[[email protected] ~]#

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

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

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

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

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# STR=ISO9001

[[email protected] ~]# case "$STR" in "$PREFIX*") echo "starts"; esac

[[email protected] ~]# case "$STR" in "$PREFIX"*) echo "starts"; esac

starts

[[email protected] ~]#

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

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

[[email protected] ~]# PREFIX=ISO

[[email protected] ~]# STR=ISO9001

[[email protected] ~]# echo "${STR#$PREFIX}"

9001

[[email protected] ~]# [ "${STR#$PREFIX}" != "$STR" ] && echo "starts with"

starts with

[[email protected] ~]#

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

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

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

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

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