-6

I am using following code to get Position of "E"

string a = "ABCDEFGHIJKLM";
int position = 0;
for (int i = 0; i < a.Length; i++)
{
    if (a.Substring(i, 1) == "E")
    {
        position = i +1;
        break;
    }
}

above code will return position = 5,

I am new to C# my question is is any fast way without loop to do this,

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Imran Ali Khan
  • 8,469
  • 16
  • 52
  • 77
  • 2
    The strange thing is that the duplicate has a similar string, the same character and even the same OP. You haven't learned a lot since then, have you? – Tim Schmelter Oct 16 '14 at 12:58
  • 1k rep but no search effort, same question posted 10 months ago, ... that's not the way dude... – xmashallax Oct 16 '14 at 12:59
  • 1
    @xmashallax: OP has asked that question himself. That's the funny thing. So he just had to search in his memory. – Tim Schmelter Oct 16 '14 at 13:00
  • Oh right ... that's even more disappointing... – xmashallax Oct 16 '14 at 13:02
  • sorry Guys, Actually I was posting to this Question on other comunnity , but due to i have habit to visit stack-over flow the question is posted here.. i was trying to delete it but already answer in 2 minutes and now it can now be delete.. – Imran Ali Khan Oct 16 '14 at 13:03
  • @ImranAliKhan, I don't get that, why would you ask a question of which you already knew the answer (from your previous question)? –  Oct 16 '14 at 13:05
  • @MrCoder it is my persnal web site to practice web things , and shair with friends i was planned to checked my friends IQ level so i was planned to post this question my web site. – Imran Ali Khan Oct 16 '14 at 13:08
  • 1
    IQ level would not be tested by this question. – Catwood Oct 16 '14 at 13:10
  • *I am new to C# my question is is any fast way without loop to do this,* suggests otherwise? –  Oct 16 '14 at 13:11
  • @MrCoder What do you mean by this.. suggests otherwise – Imran Ali Khan Oct 16 '14 at 13:13
  • *I am new to C# my question is* would suggest that you *weren't* testing friends' IQ, but then saying *this Question on other comunnity*. *checked my friends IQ level* suggests you already know the answer yourself? Is it my bad logic, or are things not ***quite*** adding up here? –  Oct 16 '14 at 13:16
  • @ImranAliKhan, Well, at least you've only the one vote I suppose... –  Oct 16 '14 at 13:17
  • @MrCoder I was already told to all that it is my web site where i was posting.. so i can use xyz user name to post, because I am admin and i can make any user which friends can not be know.. – Imran Ali Khan Oct 16 '14 at 13:18

3 Answers3

5

You can use String.IndexOf:

int position = a.IndexOf("E");

It returns the first index of the character (or sub-string) or -1 if it's not there.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Use MSDN reference String.IndexOf Method

int position = mystring.IndexOf("E");
blfuentes
  • 2,731
  • 5
  • 44
  • 72
1

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:

  1. You can learn more here
  2. 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

Community
  • 1
  • 1