Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
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

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();
 
Share this answer
 
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...
 
Share this answer
 
 
Share this answer
 
v2
Try below code:

C#
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.
 
Share this answer
 
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
 
Share this answer
 
Go with regular expression.

Try this:
C#
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"
 
Share this answer
 
it is a good practice to use a string builder instead of a string when you have to do some manipulation like this


C#
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);
 
Share this answer
 
Alternatively try searching your string then splitting it and just removing the first and last words like so:

C#
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);
 
Share this answer
 
Hello, I hole it will help:

C#
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
 
Share this answer
 
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.
 
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