0

hi all,

I am having a string. Now i need to get the last word in that string.

suppose

string text = "Hello.. how are you";

Now i need to get 'you' from that string. can anyone help me..

Thank you.

4
Contributors
4
Replies
8
Views
7 Years
Discussion Span
Last Post by niths
1

if it is separated by a space then split the string as

string text = "Hello.. how are you";
String[] splitText=text.Split(' ');
Console.WriteLine(splitText[splitText.Length-1];

this will solve your problem

Edited by abelLazm: n/a

0

Hello niths,
Words are separated by spaces. Find where the first space occur. The characters preceding the space will give you the word.
Since you want the last word of the string look for the space from the end.

>>abellazm
You are fast in typing.

Edited by Arbus: n/a

0

This is another solution:

string yourWord = text.SubString(text.LastIndexOf(" "), text.Lenght - text.LastIndexOf(" "));
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.