Replacement inside commas in C # [duplicate]

-2

Good morning everyone. I have the following string:

string frutas = "f1,f2,f3,f4,...";

Basically I want it to replace the word that is between the 3rd and 4th comma for something. for example: apple

Looking like this:

f1,f2,maçã,f4,..

How can I do this?

    
asked by anonymous 20.08.2018 / 17:32

1 answer

3

One option ...

var frutas = "f1,f2,f3,f4,...";

int posicaoSubstiuir = 3;
string valorSubstituir = "Maçã";

string[] arrFruta = frutas.Split(',');

if (arrFruta.Length >= posicaoSubstiuir)
    arrFruta[posicaoSubstiuir - 1] = valorSubstituir;

frutas = string.Join(",", arrFruta);
    
20.08.2018 / 17:50