|
string txt = "some sample text";
string lastWord = txt.Substring(txt.LastIndexOf(" ")+1);
caveat: off the top of my head, not thoroughly unit-tested
|
|
|
|
|
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++
|
|
|
|
|
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
|
|
|
|
|
System.Text.RegularExpressions.Regex.Match(input, @"\S*$");
Edit: a better solution here[^].
Eslam Afifi
modified on Sunday, September 14, 2008 11:21 AM
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
You're right.
Thank you John.
|
|
|
|
|
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 of LastIndexOf() . 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
|
|
|
|
|
John 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
|
|
|
|
|
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
|
|
|
|
|
Hey, I was wondering how I can send a file download after my class is complete.
I have a C# + ASP.NET site. I have a Button1_Click class. When that is run it copies some files, does some things and eventually creates a zip file. How do I send that zip file to the user as a download? E.G. I want it to
1) User clicks button
2) Server does what it needs to do
3) User gets a file download popup with the zip file that was created in step 2.
All I need help with is #3.
Thanks!!,
Anand
P.S. I've looked into Response.BinaryWrite but I'm not sure if that is it.
modified on Saturday, September 13, 2008 9:41 PM
|
|
|
|
|
Hey now, we have a whole ASP .NET forum y'know.
But, from that one time when I used asp .net about 3 years ago, I think you can just use Response.Redirect , the web browser should recognise that its a file and not a web page, and start a download.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
Hi there.
I have a TreeView in my project.
There are some nodes in it.
Now I want when user drags out a node to a folder, a text file creates in destination folder.
Do you understand what I mean ?
Can you help me ?
Thank you.
|
|
|
|
|
Y'know, I think I have a solution to your problem.
Step One: Create the text file in a temporary directory somewhere, and fill it with whatever you want.
Step Two: Add in something like this:
void TreeView1ItemDrag(object sender, ItemDragEventArgs e)
{
string[] stuff = new string[1];
stuff[0] = 'path to that text file you just made';
.
DataObject data = new DataObject(DataFormats.FileDrop, stuff);
.
this.treeView1.DoDragDrop(data, DragDropEffects.Move);
}
Windows will then move the text file from it's temporary home, to wherever the user dragged your node to.
My current favourite word is: Nipple!
-SK Genius
Game Programming articles start - here[ ^]-
|
|
|
|
|
|
Hi There,
I want to insert a row into a table which is displayed in datagridview.
The table is named "WireTbl" in "WireDb" database, and has 5 fields: Code, Street, Alley, Date and Time.
Would you please help me to write a correct INSERT command for this code:
Int32 x1 = System.Convert.ToInt32(textBox1.Text);//Code
String x2 = textBox2.text;//Street
String x3 = textBox3.Text;//Alley
DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date
DateTime x5 = System.Convert.ToDateTime(textBox5.Text);//Time
cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+
") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)";
Tnx
Sourie
|
|
|
|
|
use appropriate parameters to your command and change your command text according to parameter identification
i. e
Sourie wrote: cmd1.CommandText = "Insert into WireTbl("+ "Code, "+ "Street, "+ "alley, "+ "Date, "+ "Time, "+
") VALUES('" + x1 + "', "', '"+ x2+ "','"+ x3+ "','"+ x4+ "', x5)";
change to
cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";
cmd1.Parameters.Add("@Code", SqlDbType.Int).Value = x1;
cmd1.Parameters.Add("@Street", SqlDbType.Varchar).Value = x2;
cmd1.Parameters.Add("@Alley", SqlDbType.Varchar).Value = x3;
cmd1.Parameters.Add("@Date", SqlDbType.DateTime).Value = x4;
cmd1.Parameters.Add("@Time", SqlDbType.DateTime).Value = x5;
cmd1.ExecuteNonQuery();
hope it helps
dhaim
programming is a hobby that make some money as side effect
|
|
|
|
|
Thanks fort heloping me. I am using access database. Do I have to change Sql to Ole in the code you wrote me? For Example:
cmd1.CommandText = "INSERT INTO WIRETBL(Code, Street, Alley, Date, Time) VALUES(@Code, @Street, @Alley, @Date, @Time)";cmd1.Parameters.Add("@Code", OleDbType.Int).Value = x1;cmd1.Parameters.Add("@Street", OleDbType.Varchar).Value = x2;cmd1.Parameters.Add("@Alley", OleDbType.Varchar).Value = x3;cmd1.Parameters.Add("@Date", OleDbType.DateTime).Value = x4;cmd1.Parameters.Add("@Time", OleDbType.DateTime).Value = x5;cmd1.ExecuteNonQuery();
Sourie
|
|
|
|
|
Yes you have to.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
Unfortunately I got Syntax Error
Sourie
|
|
|
|
|
What error message you get?
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|
"String was not recognized as a valid DateTime." for this line:
DateTime x4 = System.Convert.ToDateTime(textBox4.Text);//Date
Then I changed DateTime to String:For example for this line:
String x5 = System.Convert.ToString(textBox5.Text);//
cmd1.Parameters.AddWithValue("@Time", OleDbType.VarChar).Value = x5;
but this time I got this error:
Syntax error in INSERT INTO statement.
Sourie
|
|
|
|
|
DateTime.ParseExact(textBox4.Text,"MM/dd/yyyy",System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat)
Sourie wrote: Syntax error in INSERT INTO statement.
Show here how do you make insert command.
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
|
|
|
|
|