Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi
we are doing search on resume text.
we done the boolean search(and,or operations).
It Is working only for my search string (textbox string) contains "java and .net and C# or Sqlserver like....
But the problem is,
if my search string starts with "and" ,ends with "and" then i want to remove first,last "and" .Don't remove the middle "and's".this is my search criteria.
 

 
for example search string is like this:
 
"and and and and and asp.net and C# and Sqlserver and java and and and and...
Then it removes start,end "and".
searching is doing like " asp.net and C# and Sqlserver and java" only.
How can get this????????
Posted 29-May-11 21:23pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 8

Here is pure LINQ version of implementing this functionality.
 
     string searchInputs = "and and and and and asp.net and C# and Sqlserver and java and and and and";
     string[] values = searchInputs.Split(' ');
     var words = values.Skip(1).Take(values.Length - 2).ToList(); // Skip the first and the last words
     StringBuilder sb= new  StringBuilder();
     foreach (var word in words)
     {
        sb.Append(word + " ");
     }
     tbSearch.Text = sb.ToString();
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Try:
    string inp = "and and and and and asp.net and C# and Sqlserver and java and and and and";
    string[] parts = inp.Split(new String[] {"and"}, StringSplitOptions.RemoveEmptyEntries);
    StringBuilder sb = new StringBuilder();
    string prefix = "";
    foreach (string s in parts)
       {
       if (!string.IsNullOrEmpty(s.Trim()))
          {
          sb.Append(prefix);
          sb.Append(s);
          prefix = "and";
          }
       }
    Console.WriteLine(sb.ToString().Trim());
It also removes duplicate "and"s in the middle...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Try below code:
 
string str = @"and java and .net and C# and Sqlserver and and and ";
            foreach (string str1 in str.Split(new string[] { "and" }, StringSplitOptions.RemoveEmptyEntries))
            {
                Console.WriteLine(str1);
            }
 

Here you don't need to worry about start/end "and" words. Let me know if it's useful to you.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hello, I hole it will help:
 
string text = "and and bla bla bla bla and bla and";
if (text.Length >= 3)
    if (text.Substring(0, 3).ToLower() == "and")
        text = text.Substring(3, text.Length - 3);
 
if (text.Length >= 3)
    if (text.Substring(text.Length - 3, 3).ToLower() == "and")
        text = text.Substring(0, text.Length - 3);
 
Console.Write(text);
 
Good Luck
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 10

Why is your search string starting or ending with an "and"? I'd look at the code you're using to create the search string and see if there isn't a way to just keep those and's from getting in there in the first place, then you won't have to worry about removing them later. If you want to post your code for that part I'm sure we could figure out a way to do it.
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Hello, you can try like this.
 
1. get the first index of "and"
2. get the last index of "and"
3. the take a substring using these indexes.
 
Good Luck
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

Go with regular expression.
 
Try this:
      string search = "and hello and my love and";
      search = System.Text.RegularExpressions.Regex.Replace(search, @"^and\s+", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
      search = System.Text.RegularExpressions.Regex.Replace(search, @"\s+and$", string.Empty, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline);
 
Output is:
 
"hello and my love"
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 7

it is a good practice to use a string builder instead of a string when you have to do some manipulation like this
 

StringBuilder inp = new StringBuilder();
               inp.Append( "and and and and and asp.net and C# and Sqlserver and java and and and and");
             
               inp.Replace("and", "", 0, 3);
               inp.Replace("and", "", inp.Length -3 , 3);
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

Alternatively try searching your string then splitting it and just removing the first and last words like so:
 
string OrigString = "and and and and and asp.net and C# and Sqlserver and java and and and and";
string SearchString = "asp.net and C# and Sqlserver and java";
string ResultString = "";
if(OrigString.Contains(SearchString))
{
    string[] StringParts = OrigString.Split(' ');
    for(int i = 0; i < StringParts.length; i++)
    {
        ResultString += StringParts[i] + " ";
    }
}
else
{
    ResultString = "No match!";
}
MessageBox.Show(ResultString);
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 321
1 Sergey Alexandrovich Kryukov 286
2 RyanDev 75
3 PhilLenoir 70
4 nv3 60
0 Sergey Alexandrovich Kryukov 6,691
1 OriginalGriff 6,119
2 CPallini 2,473
3 Richard MacCutchan 1,697
4 Abhinav S 1,560


Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 30 May 2011
Copyright © CodeProject, 1999-2014
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