天天看點

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.判斷是否以另外的字元串結尾