Replace in undefined texts

Asked

Viewed 65 times

1

I have a question regarding replace, I am populating values in word but some values I will not have available but if not fill in the word will be shown @@Variablename, so I was wondering if you have any general replace to put in the values I don’t have in @@variavelxpto the value "0,00"

if (row["id_evento"].ToString() == "8")
{
    sbConteudo.Replace("@@fdfgertyer", row["VALOR"].ToString());
}
else if (row["id_evento"].ToString() == "10")
{
    sbConteudo.Replace("@@zz", row["VALOR"].ToString());
}
else if (row["id_evento"].ToString() == "22")
{
    sbConteudo.Replace("@@xsa", row["VALOR"].ToString());
}
else if (row["id_evento"].ToString() == "24")
{
    sbConteudo.Replace("@@teste", row["VALOR"].ToString());
}
else if (row["id_evento"].ToString() == "98")
{
    sbConteudo.Replace("@@VALOR_IDES_2", row["VALOR"].ToString());
}
else if (row["id_evento"].ToString() == "99")
{
    sbConteudo.Replace("@@VALOR_IDES_1", row["VALOR"].ToString());
}

In the example I used let’s say I have the event 24,10 and 99 then will be kept the variable @@valor_ides_2 and @test, but I need to put 0 there. In some cases the variable that will not be filled will be other so I do not have a pattern.

  • Why not go through the variables instead of looking for the ids ?

  • @Rovannlinhalis thanks for the comment, I did not understand well your proposal, as would be?

  • 1

    I put the answer to how I would do it, see if it helps you

2 answers

2


I suggested you replace it with the variables.

I imagine you have a list of all the variables and which id_event should be searched when it is that variable, so you can assemble a Dictionary:

Dictionary<string,string> variaveis = new Dictionary<string,string>();
variaveis.Add("@@NOME","10");
variaveis.Add("@@TESTE","22");
variaveis.Add("@@EVENTO","24");

Then you go through the pairs of values, and exchange:

foreach (KeyValuePair<string, string> v in variaveis)
{
    string valor  = "0";
    DataRow[] rows  = dataTable.Select("id_evento = "+ v.Value); //Procura no datatable, se há registros com o respectivo id referente a variavel atual
    if (rows.Length >0) //se não houver, não altera o valor, mantendo 0
    {
       valor = rows[0]["VALOR"].ToString();
    }
    //substituio a variavel pelo valor
    sbConteudo.Replace(v.Key, valor);
}

In this way, all previously defined variables will be replaced. And when you need to add other variables, just add them to the dictionary, the rest of the code stays the same.

  • In fact the code became much cleaner and effective but unfortunately did not solve my problem, for example, if in the database ( datatable ) do not have the event 10, in my word will be @@NAME and I would like all @@. * which were not filled were 0,00. it would be like Carlos' example up there, but I’m testing because word cannot generate with the example he used.

  • In this example, if there is no id_event=10, you will put "0" in place. If @@NAME is in the list of variables, foreach will go through it and change the value.

  • Perfect Ronavann solved my doubt, that would be it. A question arose here but I will already mark that your answer is correct. It turns out that my datatable has for example event 8,10,12,14,8,14. this 8 and 14 that repeated is from the following month so if I do the same select on this datatable it will return me the value of the first 8 understand ? would have any solution for it ? example: 8,10,12,14 month December 8,14 month January but if I do this select it will return me the value of December the 2 times because it is the first.

  • Maybe if I ruled out the line after the action...

  • you have month column on datatable ?

  • I actually have the date of the movement that would be the month : (01/01/2017) and (01/01/2018)

  • But there’s a problem, man... you’re always gonna have to update your code. You have to think of something that always works... if the id_event changes you will have to change the code

  • the Id of the movement will never change, I’m sure that but the event would change, I just need to take for example 4 events of the month 01 and 2 events in the month 2 only that in the month 1 is the same Id of the 2 then I would have to remove from the data table so there is no conflict, understand?

  • I think in this case, touch the select to only bring the newest, filtering by the month

  • In a little while I’ll delete the comments

Show 5 more comments

0

You can use a regular expression:

        if (row["id_evento"].ToString() == "8")
        {
            sbConteudo.Replace("@@fdfgertyer", row["VALOR"].ToString());
        }
        else if (row["id_evento"].ToString() == "10")
        {
            sbConteudo.Replace("@@zz", row["VALOR"].ToString());
        }
        else if (row["id_evento"].ToString() == "22")
        {
            sbConteudo.Replace("@@xsa", row["VALOR"].ToString());
        }
        else if (row["id_evento"].ToString() == "24")
        {
            sbConteudo.Replace("@@teste", row["VALOR"].ToString());
        }
        else if (row["id_evento"].ToString() == "98")
        {
            sbConteudo.Replace("@@VALOR_IDES_2", row["VALOR"].ToString());
        }
        else if (row["id_evento"].ToString() == "99")
        {
            sbConteudo.Replace("@@VALOR_IDES_1", row["VALOR"].ToString());
        }

        string pattern = "@@.*";
        string replacement = "0";
        Regex rgx = new Regex(pattern);
        string sbConteudo = rgx.Replace(sbConteudo, replacement);

Browser other questions tagged

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