9
I’m developing a component to read the file sent by Dataprev with the list of monthly obituaries. This file is a TXT and every 210 characters is a different person.
The documentation can be seen in this link: SISOBI.
I’m used to separating data like this through a delimiter, using the Split()
, but this one in particular has none, and is separated by number of characters.
I made the Action
to send the file TXT for the application, and read the data contained in it.
Ex:
string exemplo = "13032015joao";
Of that string, I need to remove the data and put in variables such as:
int dia = 13;
int mes = 03;
int ano = 2015;
string nome = joao;
The number of character is fixed, example:
Day will always be 2 characters, and after it will always come the month with 2 characters, and after the year... And so until the end of 210 characters.
Using the Split()
if it had a delimiter, it would look something like this:
var exemplo = "13|03|2015|joao";
string[] stringSeparators = new string[] { "|" };
var result = nomeDescrypt.Split(stringSeparators, StringSplitOptions.None);
var dia = Convert.ToInt32(result[0]);
var Mes= Convert.ToInt32(result[1]);
var Ano= Convert.ToInt32(result[2]);
var Nome= Convert.ToInt32(result[3]);
My question is: How to separate one string, delimiting by number of characters?
Man controller to read the file is like this:
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
//verifica se o arquivo está nulo
if (file == null)
{
TempData["MensagemError"] = "Erro ao realizar o upload do arquivo!";
return View("Index");
}
//Salvar o arquivo txt
string path = Path.Combine(Server.MapPath("~/App_Data/Uploads/" + file.FileName));
file.SaveAs(path);
//Realiza a leitura do arquivo txt
var fileContents = System.IO.File.ReadAllText(path);
//testar se está lendo o arquivo
TempData["Mensagem"] = fileContents;
return RedirectToAction("Index");
}
Example of layot:
000028000280000016427201412310000000000MARCIO SEARA RIBEIRO MARIA PETANIA DE OLIVEIRA SEARA 19780306201412319442067052500000000000000000000007657
000028000290000016428201412310000000000MAIRE VALENTIM DA SILVA MAIRE VALENTIM DA SILVA 19281105201412310387867350700000000000000000000007657
What happens when the record is less than 210 characters long? The layout is filled with spaces?
– Leonel Sanches da Silva
@Ciganomorrisonmendez It will always be 210 characters, because the layout is filled with spaces or "0". I added an Example in the question.
– Randrade