EN
C# / .NET - find character index in string
0
points
In this article, we would like to show you how to find a character index in string in C#.
Quick solution:
String text = "ABC";
int result = text.IndexOf("A"); // 0
Console.WriteLine(result);
Practical example
In this example, we use String.IndexOf()
method to find character index in the text
string.
using System;
public class StringUtils
{
public static void Main(string[] args)
{
String text = "Dirask is awesome!";
int index1 = text.IndexOf("D"); // 0
int index2 = text.IndexOf("a"); // 3 (first occurrence)
int index3 = text.IndexOf("Dirask"); // 0
int index4 = text.IndexOf("is"); // 7
Console.WriteLine(index1); // 0
Console.WriteLine(index2); // 3 (first occurrence)
Console.WriteLine(index3); // 0
Console.WriteLine(index4); // 7
}
}
Output:
0
3
0
7