I have a string that goes like "abcdefg..."
I would like to find the index where the letter d is at, so I can get the number 3.
I managed to do it by looping through each letter in the string, but that doesn't sound very convenient. Is there another way?
Use the index() Function to Find the Position of a Character in a String. The index() function is used similarly to the find() function to return the position of characters in a string. Like the find() function, it also returns the first occurrence of the character in the string.
JavaScript String indexOf() The indexOf() method returns the position of the first occurrence of a value in a string. The indexOf() method returns -1 if the value is not found. The indexOf() method is case sensitive.
The String
class exposes some methods to enable this, such as IndexOf
and LastIndexOf
, so that you may do this:
Dim myText = "abcde"
Dim dIndex = myText.IndexOf("d")
If (dIndex > -1) Then
End If
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With