Replace is not working C #

1

I'm replacing one code with another in each line that the code is found. But the replace simply does not work, it goes through it and the line continues the same way. As you can see in the image below, if used within the immediate, it appears to have worked, but when checking the contents of the string I verified that it continued the same way.

stringtexto=string.Empty;intcontadorCodigos=0;intcontadorLinhas=0;stringlinha=string.Empty;string[]linhas=newstring[_linhas.Count()];foreach(stringlinein_linhas){linha=line;foreach(varitemin_codigos){if(line.Contains(item.Key)){linha.Replace(item.Key,item.Value);//line.Replace(item.Key,item.Value);texto+=$" {item.Key} - {item.Value};";
                    contadorCodigos++;
                    break;
                }
            }

            linhas[contadorLinhas] = linha;
            contadorLinhas++;
        }

        txtResultado.Text = texto;
        txtQtdCodigosTrocados.Text = contadorCodigos.ToString();

Does anyone know what might be happening?

    
asked by anonymous 10.01.2017 / 13:47

1 answer

5

The method Replace() returns a value, and this value is that you are not accumulating anywhere.

Change your line to:

linha = linha.Replace(item.Key, item.Value);

So you will accumulate the result of Replace() back in variable linha as expected.

    
10.01.2017 / 14:02