Answers for "bash index of string"

1

bash index of string

# To find the index of a substring in another string
# Take for instance, below take two strings: str and substr. 

str="hello world"
substr="world"
 
prefix=${str%%$substr*}
index=${#prefix}
 
if [[ index -eq ${#str} ]];
then
    echo "Substring is not present in string."
else
    echo "Index of substring in string : $index"
fi
Posted by: Guest on April-20-2022

Code answers related to "Shell/Bash"

Browse Popular Code Answers by Language