Replace in undefined texts

1

I have a question regarding replace, I am populating values in word but some values I will not have available but if it does not fill in the word will be shown @@ VariableName, so I wanted to know if it has some general replace to put in the values that I do not I have at @@ 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 say that I have event 24,10 and 99 then the variable @@ value_ides_2 and @@ test will be kept, but I need to put 0 there. In some cases the variable that will not be filled will be other then I do not have a default.

    
asked by anonymous 07.11.2017 / 20:55

2 answers

2

I suggested that you substitute the variables.

I imagine you have a list with all the variables and which event_id should be searched for when that variable is available, so you can set 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 value pairs, and change:

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.

    
07.11.2017 / 21:02
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);
    
07.11.2017 / 21:12