Single/First Occurrence
By using IndexOf
will return the index of the position.
using System;
class Program
{
static void Main()
{
// A.
// The input string.
const string s = "ABCDEFGHIJKLM";
// B.
// Test with IndexOf.
if (int i = s.IndexOf("E") != -1)
{
Console.Write("string contains 'E' at position of "+i);
}
Console.ReadLine();
}
}
This will output
"string contains 'E' at position of 4".
For a quick/better understanding:
- You can learn more here
- or here
The indexOf gets you the position of the character (-1 will be returned otherwise)
Multiple Occurrences:
If the char value appears multiple times throughout your string, you could use:
var foundIndexes = new List<int>();
for (int i = 0; i < myStr.Length; i++)
if (myStr[i] == 'a') foundIndexes.Add(i);
Found here