Get index back to front

Asked

Viewed 1,033 times

9

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 would like to get the index of the last.

It is necessary to consider that the string may have more spaces. The goal is to always get the last.

There is something ready in C# that does this?

1 answer

10


Use the Lastindexof(string):

string texto = "Stackoverflow em Português";

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

Output: 16

Browser other questions tagged

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