To find the first index of a substring in a string in C#, you can use the IndexOf()
method of the string
class. The IndexOf()
method takes a string parameter and returns the index of the first occurrence of the specified substring within the string. If the substring is not found, it returns -1.
Here's an example:
main.cs163 chars6 lines
In this example, we have a string str
and a substring substring
that we want to find the first index of within str
. We call the IndexOf()
method on str
and pass in substring
as a parameter. The method returns the index of the first occurrence of substring
within str
, which in this case is 16
. We then output the result to the console.
gistlibby LogSnag