Juan Salvador Portugal
Juan Salvador Portugal

Reputation: 1319

IndexOf and \ character

I'm having an issue trying to find the \ character in some string.

I have a string like this

01-TN070340000000CHEXBSPT\nTapón Galvanizado Ø3/4\" Cabeza Hexagonal. Rosca BSPT
01-TN071000000000CHEXBSPT\nTapón Galvanizado Ø1\" Cabeza Hexagonal. Rosca BSPT"
05-CBPA0010002000-002\nConjunto Aislador BT 2000. Cerámica Blanca Pasante 1 Kv. 2000A. Genérico. Ferretería Estañada. 

And all what i need is cut the string to get the string part before the \n but the thing is IndexOf() always returns -1.

Next, I tried the following:

int index;
string Articulo = "01-TN070340000000CHEXBSPT\nTapón Galvanizado Ø3/4\" Cabeza Hexagonal. Rosca BSPT";
index = Articulo.indexOf('\\'); // -1
index = Articulo.indexOf("\\n"); // -1
index = Articulo.indexOf(@"\n"); // -1

How am I supposed to get the start position of a substring that contains \ character?

Upvotes: 0

Views: 289

Answers (1)

Shmosi
Shmosi

Reputation: 332

You should use .IndexOf('\n') because \n is handled as one char.

One of your problems was to use @ which made the outcome not newline, but \n as two chars, same with \\.

That is also the reason you can't find just \ in your string, because it goes hand in hand with the n.

Upvotes: 1

Related Questions