Click here to Skip to main content
15,894,017 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Hi how to remove last strings in textbox.


if my search criteria is like .net and asp.net and and and and....
then i want to remove last strings(i.e last and's only).
so how to remove last strings in textbox
Posted
Comments
yesotaso 29-May-11 5:21am    
Please confirm the required output. What do you mean by "last stringS(i.e last and'S"?
".net and asp.net and and and" would be modified to ".net and asp.net and and" or ".net and asp.net"
Member 7932936 30-May-11 0:55am    
no.i want to remove last "and"

Use Substring

search on net


C#
string s = ".net and asp.net";
string output = s.Replace("and", "");
 
Share this answer
 
v2
Comments
Member 7932936 27-May-11 6:22am    
yes.i used substring but it works only to remove one "and" only.i want to remove many "and"'s in my string.then how to remove many "and"'s
Ashishmau 27-May-11 6:25am    
So,in that case, u can use replace method. see my improved answer
Tarun.K.S 27-May-11 6:40am    
OP wants to remove the last string's, you are removing all of them.
Ashishmau 27-May-11 6:43am    
First of all, Read his comment carefully before downvoting..
Tarun.K.S 27-May-11 6:53am    
And I would like you to read his original question.
Hi,
Please try String.TrimEnd

C#
string s = @".net and asp.net and" ;
string output = s.TrimEnd("and");
 
Share this answer
 
Comments
Tarun.K.S 27-May-11 7:17am    
I think it should be string output = s.TrimEnd(new char[]{'a','n','d'});
Member 7932936 27-May-11 8:04am    
it is also showing error like index was out of bound arrey exception
Member 7932936 30-May-11 1:13am    
string output = s.TrimEnd("and");
This removes only last "and".But I Want to Remove all last "and".
That means if my string ends with and and and and and and .....then i want to remove all last "and".How can remove ????
Use regular expression string: and$
C#
string query = Regex.Replace(".net and asp.net and", @"and$", string.Empty, RegexOptions.IgnoreCase | RegexOptions.Singleline);


Output will be:

".net and asp.net "

Or replace regular expression string to \w+$, this will find the last word in a string.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900