Gabriel76
Gabriel76

Reputation: 23

Subtring with a (+) in the string

i have the following string in a file

"  14962  1250UI Iny. F.Amp. x 1 + Diluy.+ Aguja                          51890510   7798006880162B"

when i read this with this code

string lectura, sinuso = string.Empty;    
string baspre = @"U:\baspre.txt";
 
 FileStream fsInPre = File.OpenRead(baspre);
 StreamReader sr = new StreamReader(fsInPre, System.Text.Encoding.UTF7);

 lectura = sr.ReadLine();

 while (sr.Peek() > -1)
{
 des = lectura.Substring(10, 60).Trim();
 sinuso = lectura.Substring(70, 27);
  ....
  ....
 }

then i can see that Readline parse the string as

" 14962 1250UI Iny. F.Amp. x 1 Diluy. Aguja 51890510 7798006880162B"

because of the "+" so this is not the correct string and its short than the original so
i get the following error in the sinuso line because i cant take 27 chars

(index and length must refer to a location within the string)

i need the exact subtring. How can i do that?

thx.

Upvotes: 1

Views: 71

Answers (1)

Blas Ortellado
Blas Ortellado

Reputation: 76

Try this:

string lectura, sinuso, des = string.Empty;
string baspre = @"baspre.txt";

FileStream fsInPre = File.OpenRead(baspre);
StreamReader sr = new StreamReader(fsInPre, System.Text.Encoding.UTF8);



while (sr.Peek() > -1) {
    lectura = sr.ReadLine();
    des = lectura.Substring(10, 60).Trim();
    sinuso = lectura.Substring(70, 27);
    System.Console.WriteLine(des);
    System.Console.WriteLine(sinuso);
    // and beyond
}

Upvotes: 1

Related Questions