hmdb
hmdb

Reputation: 323

getting an index of a character

I'm having a problem getting an index of the character '-' of a string. Let me explain my problem. First I have to read a line from a text file. The only line in the text file is "Beta = 62.5 * (Sigma – Delta) / 125"

StreamReader rdr = new StreamReader(openPath, Encoding.Default); 

while (rdr.Peek() != -1)
{
     string strInput = rdr.ReadLine();
}

then I need to get the index of char '-'.

int col = strInput.IndexOf('-');

After the above line 'col' is equal to -1. But as you can see the '-' character is in the above mentioned string which read from the text file. I couldn't figure out why I'm getting -1 as the index of '-'. help me...

Upvotes: 1

Views: 568

Answers (3)

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

Those two characters are not the same. Look at the length of them:

(this is called an en-dash, equal to the width of the ASCII character N)

vs

-

(this is a normal ascii hyphen, or minus sign)

Amend your indexof to use the en-dash (–) instead of the hyphen (-) and you should get proper results.

Edit: Thanks to sixlettervariables for correct terminology

Upvotes: 2

Mentoliptus
Mentoliptus

Reputation: 2985

You are not searching for the same character, the character you are searching for should be '–' and not '-'

Try his line:

int col = a.IndexOf('–');

Upvotes: 1

Daniel Daranas
Daniel Daranas

Reputation: 22624

Add (temporarily) a line to your code, just before int col = strInput.IndexOf('-');:

System.Diagnostics.Debug.Assert(String.Equals(strInput, "Beta = 62.5 * (Sigma – Delta) / 125");

Upvotes: 0

Related Questions