how can i get the last word from the string
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
string txt = "some sample text"; string lastWord = txt.Substring(txt.LastIndexOf(" ")+1); caveat: off the top of my head, not thoroughly unit-tested :-D
Best regards, Steven A. Lowe CEO, Innovator LLC www.nov8r.com
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
ahmedhassan96 wrote:
i want know hot can i get the last word from string which inputed from user
Two ways I can think of immediately: String.Split() or String.LastIndexOf() and String.Substring()
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
split the string into an array using a space as the delimiter and get the last element in the array or identify the last space in the string and get the trailing characters
Never underestimate the power of human stupidity RAH
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
Try this :
static void Main()
{
string temp = "my favorite song is ttt.mp3";
Console.WriteLine("Last word = {0}", Regex.Match(temp.Trim(), @"[^\s]*$" ).ToString() ) ;
Console.ReadKey();
} -
Try this :
static void Main()
{
string temp = "my favorite song is ttt.mp3";
Console.WriteLine("Last word = {0}", Regex.Match(temp.Trim(), @"[^\s]*$" ).ToString() ) ;
Console.ReadKey();
}I hate people who know regex - what an arcane, completely undecipherable load of... Take 5
Never underestimate the power of human stupidity RAH
-
Try this :
static void Main()
{
string temp = "my favorite song is ttt.mp3";
Console.WriteLine("Last word = {0}", Regex.Match(temp.Trim(), @"[^\s]*$" ).ToString() ) ;
Console.ReadKey();
}Why burden the app with regex? Someone else gave what is, in my humble aopinion, a much better solution using
LastIndexOf(" ")
."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
Why burden the app with regex? Someone else gave what is, in my humble aopinion, a much better solution using
LastIndexOf(" ")
."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001You're right. Thank you John.
-
Why burden the app with regex? Someone else gave what is, in my humble aopinion, a much better solution using
LastIndexOf(" ")
."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001In my humble opinion, the
LastIndexOf(" ")
solution will fail in case of other white space characters or if the string contains one word only. In fact an expression like@"\S*(?=\s*$)"
would be better as it matches the last non-whitespace character combination in the string.Eslam Afifi
-
In my humble opinion, the
LastIndexOf(" ")
solution will fail in case of other white space characters or if the string contains one word only. In fact an expression like@"\S*(?=\s*$)"
would be better as it matches the last non-whitespace character combination in the string.Eslam Afifi
The OP's example CLEARLY indicatedc spaces were being used to separate words in a TextBox. What other kind of whitespace characters did you have in mind? First, using RegEx obfuscates the intent of the code, and slows it down besides. I can count on ONE HAND the number of people I know that would use
RegEx
over the much simpler construct ofLastIndexOf()
. As soon as new programmers learn that readability and maintainability is much more important than "clever code", we'll all be a lot better off. Sure,RegEx
has it's place, but not for this problem. A valid example of requiring RegEx is validating that an email address is properly formatted. But for finding the last word in a string of words separated by spaces? Not on your life."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001 -
The OP's example CLEARLY indicatedc spaces were being used to separate words in a TextBox. What other kind of whitespace characters did you have in mind? First, using RegEx obfuscates the intent of the code, and slows it down besides. I can count on ONE HAND the number of people I know that would use
RegEx
over the much simpler construct ofLastIndexOf()
. As soon as new programmers learn that readability and maintainability is much more important than "clever code", we'll all be a lot better off. Sure,RegEx
has it's place, but not for this problem. A valid example of requiring RegEx is validating that an email address is properly formatted. But for finding the last word in a string of words separated by spaces? Not on your life."Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001John Simmons / outlaw programmer wrote:
The OP's example CLEARLY indicatedc spaces were being used to separate words in a TextBox.
but the question stated
ahmedhassan96 wrote:
hot can i get the last word from string which inputed from user
and he didn't explicitly mention the delimiter used. Your post made me think of other white space characters such as Tab and NewLine (depends on how he gets the input string from the user). I'm aware that readability and maintainability is more important than clever code, and i wasn't trying to be clever. I just think a simple regex is much less in code than using LastIndexOfAny and handling the -1 return case.
Eslam Afifi
-
hi every body i want know hot can i get the last word from string which inputed from user example: user entered:"my favorite song is ttt.mp3"; my question is how can i get "ttt.mp3" from the string thanx every one
It will be much more easy and simple to use 'lastindexof' So you can use this: string lastword; int lastindex = str.LastIndexOf(" ")-1; int len = str.Length; if (lastindex == -1) //if there is only one word in the string { lastword = str; } else { lastword = str.Substring(lastindex, (len - lastindex)); } //here str is the string from which last word has to be taken