indefof
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
Ugliest Unicorn
const beasts = ['ant', 'bison', 'camel', 'duck', 'bison'];
console.log(beasts.indexOf('bison'));
// expected output: 1
# 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
sentence = 'Python programming is fun.'
result = sentence.index('is fun')
print("Substring 'is fun':", result)
result = sentence.index('Java')
print("Substring 'Java':", result)
// applies to C#
string S = "Any String At All"
int Sindex = S.indexOf("A") // accepts string or char arguments.
// "Sindex" would be equal to "0", as the indexOf(); meathod returns
// the index position of the first occurance of the specified argument.
s = 'Hello'
len(s) ## 5
## Chars are numbered starting with 0
s[0] ## 'H'
s[1] ## 'e'
s[4] ## 'o' -- last char is at length-1
s[5] ## ERROR, index out of bounds