Split by modifying the sorting of the text file

Asked

Viewed 32 times

0

I am loading a text file in a string [], later making a split because the file is separated by spaces, but I noticed that lines are with different sizes, this is hindering the capture of positions in each one of the lines of the file, in the example code, I can only capture the text of position 65, when in fact the correct position was 231 until 239, the file in Notepad is correct, however, inside the code not.

public static string CarregaTxt()
{
string[] array = File.ReadAllLines(@"C:\Users\aoliveira\source\repos\BRR23120.ret");

string resultado = "";
for (int i = 0; i < array.Length; i++)
{                  
resultado = array[i];
string[] returnDataSplited = resultado.Split(' ');
string code = returnDataSplited[65];
var data = GetReturnMessage(code);
Console.WriteLine(resultado);

}
return resultado.ToString();
}
  • Add at least the sample data line where you find the error and another where it is correct.

1 answer

0


Using the code below will not take a substring from your original string (resultado).

resultado = array[i];
string[] returnDataSplited = resultado.Split(' ');
string code = returnDataSplited[65];

If you want to get the text from position 231 to 239 vc you should use the method substring. The first attribute is the initial position (231, in your case - but note that the count starts at zero) and the second is the size of the text you want to get (8, for example).

resultado = array[i];
string code = resultado.Substring(231, 8);
  • Thank you tvdias, unfortunately had only gotten negative answers, but his answer was simple and direct, solved the problem. I also had another idea to remedy this problem, but with substring it was much better.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.