日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發布時間:2023/12/9 java 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

From:?http://codingstandards.iteye.com/blog/1187353

In Java

String.endsWith

oolean ??? endsWith(String suffix)
????????? 測試此字符串是否以指定的后綴結束。

?

StringUtils.endsWith & StringUtils.endsWithIgnoreCase & StringUtils.endsWithAny

org.apache.commons.lang.StringUtils endsWith方法 寫道 public static boolean endsWith(String str, String suffix)

Check if a String ends with a specified suffix.

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

StringUtils.endsWith(null, null) = true
StringUtils.endsWith(null, "def") = false
StringUtils.endsWith("abcdef", null) = false
StringUtils.endsWith("abcdef", "def") = true
StringUtils.endsWith("ABCDEF", "def") = false
StringUtils.endsWith("ABCDEF", "cde") = false


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

?

org.apache.commons.lang.StringUtils endsWithIgnoreCase方法 寫道 public static boolean endsWithIgnoreCase(String str, String suffix)

Case insensitive check if a String ends with a specified suffix.

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

StringUtils.endsWithIgnoreCase(null, null) = true
StringUtils.endsWithIgnoreCase(null, "def") = false
StringUtils.endsWithIgnoreCase("abcdef", null) = false
StringUtils.endsWithIgnoreCase("abcdef", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "def") = true
StringUtils.endsWithIgnoreCase("ABCDEF", "cde") = false


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

?

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

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

StringUtils.endsWithAny(null, null) = false
StringUtils.endsWithAny(null, new String[] {"abc"}) = false
StringUtils.endsWithAny("abcxyz", null) = false
StringUtils.endsWithAny("abcxyz", new String[] {""}) = true
StringUtils.endsWithAny("abcxyz", new String[] {"xyz"}) = true
StringUtils.endsWithAny("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 ends with any of the the prefixes, case insensitive, or both null ?

In Bash

使用[[ ]] 模式匹配來判斷是否以別的字符串結尾(推薦方式)

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

?

[root@web ~]#?STR=hello.gif?
[root@web ~]#?SUFFIX=.gif?
[root@web ~]#?[[ $STR == *$SUFFIX ]] && echo "ends"?
ends
[root@web ~]#?

?

使用[[ ]] 正則表達式匹配來判斷是否以別的字符串結尾

格式:[[ $STR =~ $SUFFIX$ ]]

在正則表達式中,$匹配結尾。

?

[root@web ~]#?STR=hello.gif?
[root@web ~]#?SUFFIX=.gif?
[root@web ~]#?[[ $STR =~ $SUFFIX$ ]] && echo "ends"?
ends
[root@web ~]#?

?

例外:在上面的例子中,SUFFIX中包含點(.),而點(.)在正則表達式中能夠匹配任意字符,如下所示:

[root@web ~]#?STR=helloxgif?
[root@web ~]#?SUFFIX=.gif?
[root@web ~]#?[[ $STR =~ $SUFFIX$ ]] && echo "ends"?
ends
[root@web ~]#

?

用case語句來判斷是否以別的字符串結尾

正確:case "$STR" in *"$SUFFIX") echo "ends"; esac

錯誤:case "$STR" in "*$SUFFIX") echo "ends"; esac

注意*不能寫在雙引號里面,否則不靈。

?

?

[root@web ~]#?STR=hello.gif?
[root@web ~]#?SUFFIX=.gif?
[root@web ~]#?case "$STR" in *"$SUFFIX") echo "ends"; esac?
ends
[root@web ~]#?case "$STR" in "*$SUFFIX") echo "ends"; esac?
[root@web ~]#

?

用去尾法判斷是否以別的字符串結尾

格式:[ "${STR%$SUFFIX}" != "$STR" ]

?

[root@web ~]#?STR=hello.gif?
[root@web ~]#?SUFFIX=.gif?
[root@web ~]#?[ "${STR%$SUFFIX}" != "$STR" ] && echo "ends"?
ends
[root@web ~]#


總結

以上是生活随笔為你收集整理的Bash字符串处理(与Java对照) - 17.判断是否以另外的字符串结尾的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。