Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
3.40/5 (5 votes)
Hi i want to remove last words in my textbox.
we are doing the application for resume search.
If my search string(textbox) contains .net and asp.net and and and and.....
Then I want to remove last "ands" and search is to done like ".net and asp.net" only.
How can i remove last words





I used substring and trimend.but it removes only one last "and"
Posted
Comments
Sergey Alexandrovich Kryukov 30-May-11 1:39am    
How about not writing all those end end end? :-)
If you can remove one "end", why the rest of them is a problem?
--SA
Member 7932936 30-May-11 1:47am    
it removes only last "and".fine.But the error shows like index was outside the bounds of an array
parmar_punit 31-May-11 3:14am    
Hi Member 7932936
check my updated solution.. now it works fine

Use regular expression string: and$
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.


If u want to remove only last "and" then you could use this
<pre lang="cs">string s = @".net and asp.net and" ;
string output = s.TrimEnd("and");

 
Share this answer
 
v2
Comments
Member 7932936 30-May-11 2:28am    
if my string is like ".net and asp.net and and and and and".It is not working.
It is working only for ".net and asp.net and" .
Sergey Alexandrovich Kryukov 31-May-11 0:11am    
You can probably modify it to work. And my algorithm certainly works.
--SA
C#
private string strcut(string str)
{
    string[] a = str.Trim().Split(' ');
    string str1 = string.Empty;
    for (int i = 0; i < a.Count() - 1; i++)
    {
        str1 = str1 + a[i];
        if (a.Count() - 2 != i)
        { str1 += " "; }
    }
    return str1;
}


C#
private void button1_Click(object sender, RoutedEventArgs e)
{
    string str = textBox1.Text;
    MessageBox.Show(strcut(str));
}
 
Share this answer
 
v2
Comments
Member 7932936 30-May-11 2:34am    
This code is not working.
this code is removing only spaces between the string.
But i want to remove last "and's" in my string
Sergey Alexandrovich Kryukov 31-May-11 0:11am    
OP is right, my my algorithm will work, please see.
--SA
Well remove them using string.TrimEnd until you're done. The criteria is that the last trim does not change the length of the stream — it means nothing was trimmed, so you're done.

See:

C#
static string TrimRightWord(string src, string toTrim) {
    if (src.EndsWith(toTrim))
        return src.Substring(0, src.Length - toTrim.Length);
    return src;
}

static string TrimEnds(string src, string wordToRemove) {
    char[] trimChars = new char[] {' '};
    int len;
    do {
        len = src.Length;
        src = src.TrimEnd(trimChars);
        src = TrimRightWord(src, wordToRemove);
        src = src.TrimEnd(trimChars);
    } while (len != src.Length);
    return src;
}


Better now?

—SA
 
Share this answer
 
v4
Comments
[no name] 30-May-11 23:49pm    
Hi SA,
This code will not remove all 'ands' at the end. this will remove only the last 'and'
Sergey Alexandrovich Kryukov 31-May-11 0:08am    
Wrong. It will remove all of them. Trim by " " and "end" until not trimmed. What' so difficult?
--SA
yesotaso 31-May-11 7:20am    
I cant imagine what downvoter was thinking :( apart from the typo there "end" => "and" it looks legit answer. Though I wouldn't use that logic and remove piece of input which may serve both "logical operator" and/or "the data itself", think of input "beast and beauty" the word "and" may serve both operator and input here. It might be better to construct eval tree from input text.
Sergey Alexandrovich Kryukov 31-May-11 19:01pm    
It's not even a type. Who cares what word is that -- the code is abstracted from the word; OK, I replaced it with a parameter; thank you.
I don't discuss semantic of this code -- it makes no sense because the problem itself has no sense. To start with, those stupid repeated words should not be there. The whole idea indicate some bad approach, I don't even know the idea.
I feel sorry in most cases I try to answer any kind of stupid questions; when I simply explain why it's stupid, it is often accepted.
I violate my favorite principle: "don't be a problem solver". Better be not.
--SA
Monjurul Habib 31-May-11 19:07pm    
i do agree with SA.my 5.
VB
Dim result As String
   Dim stringToTrim As String = ".net and asp.net and and and and and"
   Private Sub MyMethod()
       TrimEnd(stringToTrim)
       MsgBox(result)
   End Sub
   Private Sub TrimEnd(ByVal input As String)
       If String.IsNullOrEmpty(result) Then
           Dim length = input.Trim.LastIndexOf(" and")
           If length + 4 = input.Trim.Length Then
               TrimEnd(input.Substring(0, length))
           Else
               result = input
           End If
       End If
   End Sub


The result will be .net and asp.net
 
Share this answer
 
v2
VB
Dim result As String
   Dim stringToTrim As String = ".net and asp.net and and and and and"
   Private Sub MyMethod()
       TrimEnd(stringToTrim)
       MsgBox(result)
   End Sub
   Private Sub TrimEnd(ByVal input As String)
       If String.IsNullOrEmpty(result) Then
           Dim length = input.Trim.LastIndexOf(" and")
           If length + 4 = input.Trim.Length Then
               TrimEnd(input.Substring(0, length))
           Else
               result = input
           End If
       End If
   End Sub


The result will be .net and asp.net
 
Share this answer
 
to do such manipulation use a stringbuilder class
C#
StringBuilder inp = new StringBuilder();
               inp.Append( "asp.net and C# and Sqlserver and java and and and and");
             
               
               while (inp.ToString().Substring(inp.Length - 4, 4) == " and")
               {
                   inp.Replace(" and", "", inp.Length - 4, 4);
               }
 
Share this answer
 
Here ya go...

Method to trim given text from the end of your string...

C#
private void TrimExcessTextFromEnd(ref string src, string text)
{
    if (src != null && src.Length >= text.Length && src.Contains(text))
    {
        src = src.Trim().TrimEnd(text.ToCharArray());

        while (src.Trim().EndsWith(text))
            TrimExcessTextFromEnd(ref src, text);
    }
}


... Test It ...

C#
string src = "c# and asp.net and and and and";
TrimExcessTextFromEnd(ref src, "and");
MessageBox.Show(src);
 
Share this answer
 
You can try this

string str=".net and asp.net and";

Messagebox.show(str.Substring(0,str.LastIndexOf("and"));
 
Share this answer
 
v2
C#
string s=".net and asp.net and and and and and";
string output=s.Replace("and",String.Empty); 
 
Share this answer
 
Comments
yogiCsharp 30-May-11 23:51pm    
This will remove all the ands and output will be ".net asp.net",
this is only the half work. We should add ands after this between every entity like
".net and asp.net"
Sergey Alexandrovich Kryukov 31-May-11 0:10am    
Correct, it won't work, but my algorithm will.
--SA
Monjurul Habib 31-May-11 19:08pm    
agree.
Hi,

Take the entire string and split it using the string.split function by giving the <space>and<space> as the splitter string this will ensure that the desired strings are in your array.iterate them and add all the strings to the result.
for example

string sSearchQuery = "asp.net and C# and SQL SERVER and ORACLE and and and and and";

string[] searchTerms = sSearchQuery.Split(" and ");
StringBuilder sbFinalSearchString =new StringBuilder();
for(int index = 1;index<searchterms.count;index++)>
{
      if(index!=1 && index!=searchterms.count)
       {
         sbFinalSearchString.Append(" and ");
       }
     sbFinalSearchString(searchTerms[index-1]);
}


This code has not been tested but please give it a try before working
 
Share this answer
 

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