Post-comma replacement in C#

Asked

Viewed 76 times

0

Guys I’m using Visual Studio 2015 and would like and would like to replace a text for after the second comma. If the ID is equal (in case 501) it replaces the translation after 2° (and before 3°)

Example:

Archive with the translations:

501
Poção Vermelha

File to be translated:

501,Red_Potion,Red Potion,xxx

Final result:

501,Red_Potion,Poção Vermelha,xxx

The comma-free file is in Textbox1 and the comma file is in Textbox2

  • What you are actually wanting to do, there are other more appropriate ways for you to handle languages and translations. https://msdn.microsoft.com/en-us/library/fw69ke6f.aspx

  • @Leandroangelo I want to do using the replace method

  • You cannot do as an array?

  • That’s why this is a large text I’m editing. Each line is an element of an array.

2 answers

0


In a simple way you can do this with a Split:

string[] split = textBox1.Text.Split(',');
split[2] = "Poção Vermelha";
textBox2.Text = string.Join(",", split);

0

Try this:

// traducoes
var traducoes = new List<String> { "501", "Poção Vermelha" };

// texto
var linha = "501,Red_Potion,Red Potion,xxx";

//divide os itens por vírgula
var itens = linha.Split(',');

//id para procurar no arquivo de tradução
var id = itens[0];

//pega o índice do id nas traduções
var indexId = traducoes.IndexOf(id);

//verifica se achou o id nas traduções e se existe uma linha após ele
if(indexId != -1 && indexId + 1 < traducoes.Count)
{
    //tradução encontrada pelo id no arquivo           
    var traducao = traducoes[indexId + 1];

    //faz a troca
    itens[2] = traducao;

    //une as partes novamente
    linha = string.Join(",", itens);
}

Browser other questions tagged

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