Click here to Skip to main content
15,867,771 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hi if my search string contains multiple and,multiple or

we are replacing and,or with space.


for example:- search string like "andandandandjavaandandand".
we are removing all and.(then search string is "java".
OR
and and and and and and java or or or orororor.here also replacing all and,or with space.

upto this it is working fine.

But the problem is if my search word is like "android"
then i don't want to remove "and"

if like "configurator".in this i don't want to remove or.because it is a meaningful word.i want to search on that word.


this is my code

C#
if (TextBox1.Text.EndsWith("and"))
                {
                    TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 3);
                    TextBox1.Text.Trim();
                }
                if (TextBox1.Text.EndsWith("or"))
                {
                    TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2);
                    TextBox1.Text.TrimEnd();
                }
                if (TextBox1.Text.StartsWith("and"))
                {
                    TextBox1.Text = TextBox1.Text.Remove(0, 3);
                }
                if (TextBox1.Text.StartsWith("or"))
                {
                    TextBox1.Text = TextBox1.Text.Remove(0, 2);
                }



how can i write code for this
Posted
Comments
RakeshMeena 3-Jun-11 7:18am    
I remember you posted similar questions earlier also. It's not always feasible to code for all the probable situations. You might want to educate the user to properly enter the search criteria.
Sergey Alexandrovich Kryukov 3-Jun-11 23:54pm    
You are right. Bad idea, but not because of educating users or not. Application show act as a compiler: never modify input text but parse it and show warnings, errors, etc. Not very easy to implement, but a must.
--SA
Ra-one 3-Jun-11 8:36am    
Its a bad practice to code like this. What is the actual reason to remove and & or. If "AND" & "OR" are attached with any string it will not affect query or any manipulation.
Sergey Alexandrovich Kryukov 3-Jun-11 23:54pm    
Absolutely bad, I agree. See my comment above.
--SA

1 solution

try this...

if (TextBox1.Text.EndsWith(" and") || TextBox1.Text.EndsWith("and ") || TextBox1.Text.EndsWith("andand"))
{
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 3);
TextBox1.Text.Trim();
}
if (TextBox1.Text.EndsWith(" or") || TextBox1.Text.EndsWith("or ") || TextBox1.Text.EndsWith("oror"))
{
TextBox1.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 2);
TextBox1.Text.TrimEnd();
}
if (TextBox1.Text.StartsWith("and") || TextBox1.Text.StartsWith("and ") || TextBox1.Text.StartsWith("andand"))
{
TextBox1.Text = TextBox1.Text.Remove(0, 3);
}
if (TextBox1.Text.StartsWith("or") || TextBox1.Text.StartsWith("or ") || TextBox1.Text.StartsWith("oror"))
{
TextBox1.Text = TextBox1.Text.Remove(0, 2);
}
 
Share this answer
 
v4
Comments
RakeshMeena 3-Jun-11 7:32am    
There can be n number of combination to junk text, so are you suggesting that we need to write those many "if" statements? I would say "Bad Coding"!
Sergey Alexandrovich Kryukov 3-Jun-11 23:50pm    
Of course you are right. It's unacceptably bad. My 1.
--SA

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


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