substrings...
-
may i ask how do i get the last word out from the string using a substring ?? say the string would be ... "Hi how are you" and i would like to take the you out ... and use it ... ?? thanks for the help ...
You can use
LastIndexOf(" ")
on the string to find the index of the last space character. Then you can useSubstring(index+1)
to get the last word in the string. Does this help?
Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
-
You can use
LastIndexOf(" ")
on the string to find the index of the last space character. Then you can useSubstring(index+1)
to get the last word in the string. Does this help?
Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
-
may i ask how do i get the last word out from the string using a substring ?? say the string would be ... "Hi how are you" and i would like to take the you out ... and use it ... ?? thanks for the help ...
string text = "Hi how are you";
string lastWord = text.Substring(text.LastIndexOf(' ') + 1);Regards, Alvaro
-
LastIndexOf(" ") returns the last index of the string, in this case a space. You supply this index to the Substring() method, you need to add the one in order to move the index on to the first character of the last word, otherwise your result string will start with a space.
int index = myString.LastIndexOf(" ");
string result = myString.Substring(index+1);Does this help?
Cada uno es artifice de su ventura WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and Forums
-
string text = "Hi how are you";
string lastWord = text.Substring(text.LastIndexOf(' ') + 1);Regards, Alvaro
you should have double quotes not single
string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);
-
you should have double quotes not single
string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);
-
you should have double quotes not single
string text = "Hi how are you";string lastWord = text.Substring(text.LastIndexOf(" ") + 1);
-
You can use either, the method's overloaded, so one version takes a string ( " " ), another a char ( ' ' ). The char method in my opinion would be the right one to go for in this instance. Regards Russell