Substitution within commas in C#

Asked

Viewed 34 times

-2

Good morning, everyone. I have the following string:

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

Basically I want him to replace the word that is between 3°and 4°; comma with something. for example: apple

Thus remaining:

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

How can I do?

  • Try using split(), https://msdn.microsoft.com/pt-br/library/tabh47cf(v=vs.110). aspx makes the changes you want in the vector and then puts it back together again

  • what have you tried to do? you can try it with split (as mentioned above), substring, regex.. but you need to show something so we can help..

  • And what differs from your previous question?

  • I will try the split. I was more specific in this question.

1 answer

3


An 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);

Browser other questions tagged

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