“String -Index” Code-Antworten

Index der Zeichenfolge

# 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

Mckynde

Python String Index von

sentence = 'Python programming is fun.'

result = sentence.index('is fun')
print("Substring 'is fun':", result)

result = sentence.index('Java')
print("Substring 'Java':", result)
Healthy Heron

String -Index

  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
Precious Parrot

Ähnliche Antworten wie “String -Index”

Fragen ähnlich wie “String -Index”

Weitere verwandte Antworten zu “String -Index” auf Python

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen