Get index backwards


Imagine I have the following string:

string texto = "Stackoverflow em Português";

If I want to know the index of the first space, just:

int index = texto.IndexOf(" ");

But in this case I have two spaces, and I would like to get the index of the last one.


Note that the string may have more spaces. The goal   is to always get the latter.

Is there anything ready in C # to do this?

asked by anonymous 18.11.2015 / 11:30

1 answer


Use the LastIndexOf (string) :

string texto = "Stackoverflow em Português";

Console.WriteLine(texto.LastIndexOf(" "));

Output: 16

18.11.2015 / 11:33