Languages
[Edit]
EN

C# / .NET - find character index in string

0 points
Created by:
Trenton-McKinney
618

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
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - find character index in string

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join