I'm developing a component to read the file submitted 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 at this link : SISOBI .
I'm used to separating data like this through a delimiter, using Split()
, but this in particular does not have any, and is separated by amount of characters.
I made Action
to send the TXT file to the application, and read the data contained therein.
Ex:
string exemplo = "13032015joao";
From this string , I need to remove the data and put it in variables like:
int dia = 13;
int mes = 03;
int ano = 2015;
string nome = joao;
The amount of character is fixed, eg:
Day will always be 2 characters, and after it will always come the month with 2 characters, and after the year ... And so on until the 210 characters finish.
Using 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 a string , delimiting by amount of characters?
My controller to read the file looks 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");
}
Layout example:
000028000280000016427201412310000000000MARCIO SEARA RIBEIRO MARIA PETANIA DE OLIVEIRA SEARA 19780306201412319442067052500000000000000000000007657
000028000290000016428201412310000000000MAIRE VALENTIM DA SILVA MAIRE VALENTIM DA SILVA 19281105201412310387867350700000000000000000000007657