How to check if a string contains a substring in Bash

后端 未结 26 2524
慢半拍i
慢半拍i 2020-11-22 01:58

I have a string in Bash:

string=\"My string\"

How can I test if it contains another string?

if [ $string ?? \'foo\' ]; then         


        
26条回答
  •  面向向阳花
    2020-11-22 02:07

    Try oobash.

    It is an OO-style string library for Bash 4. It has support for German umlauts. It is written in Bash.

    Many functions are available: -base64Decode, -base64Encode, -capitalize, -center, -charAt, -concat, -contains, -count, -endsWith, -equals, -equalsIgnoreCase, -reverse, -hashCode, -indexOf, -isAlnum, -isAlpha, -isAscii, -isDigit, -isEmpty, -isHexDigit, -isLowerCase, -isSpace, -isPrintable, -isUpperCase, -isVisible, -lastIndexOf, -length, -matches, -replaceAll, -replaceFirst, -startsWith, -substring, -swapCase, -toLowerCase, -toString, -toUpperCase, -trim, and -zfill.

    Look at the contains example:

    [Desktop]$ String a testXccc
    [Desktop]$ a.contains tX
    true
    [Desktop]$ a.contains XtX
    false
    

    oobash is available at Sourceforge.net.

提交回复
热议问题