Click here to Skip to main content
12,510,824 members (51,587 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET SQL-Server , +
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 29-May-11 19:35pm
Comments
SAKryukov 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@yahoo.co.in 31-May-11 3:14am
   
Hi Member 7932936
check my updated solution.. now it works fine
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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");
  Permalink  
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" .
SAKryukov 31-May-11 0:11am
   
You can probably modify it to work. And my algorithm certainly works.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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;
}

private void button1_Click(object sender, RoutedEventArgs e)
{
    string str = textBox1.Text;
    MessageBox.Show(strcut(str));
}
  Permalink  
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
SAKryukov 31-May-11 0:11am
   
OP is right, my my algorithm will work, please see.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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:

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
  Permalink  
v4
Comments
Sangunni 30-May-11 23:49pm
   
Hi SA,
This code will not remove all 'ands' at the end. this will remove only the last 'and'
SAKryukov 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.
SAKryukov 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.
SAKryukov 31-May-11 20:05pm
   
Thank you, Monjurul.
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

to do such manipulation use a stringbuilder class
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);
               }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 10

Here ya go...

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

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 ...

string src = "c# and asp.net and and and and";
TrimExcessTextFromEnd(ref src, "and");
MessageBox.Show(src);
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 11

You can try this

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

Messagebox.show(str.Substring(0,str.LastIndexOf("and"));
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

string s=".net and asp.net and and and and and";
string output=s.Replace("and",String.Empty); 
  Permalink  
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"
SAKryukov 31-May-11 0:10am
   
Correct, it won't work, but my algorithm will.
--SA
Monjurul Habib 31-May-11 19:08pm
   
agree.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 9

Hi,

Take the entire string and split it using the string.split function by giving the
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160929.1 | Last Updated 1 Jun 2011
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100