Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the index of a char in string?

Tags:

string

vb.net

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?

like image 654
Voldemort Avatar asked Aug 20 '11 19:08

Voldemort


People also ask

How do you find the index of a character in a string in Python?

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.

How do you find the index of a particular character in a string in JavaScript?

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.


1 Answers

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
like image 62
Grant Thomas Avatar answered Nov 06 '22 00:11

Grant Thomas