to reduce the no of lines
is there a way to reduce the below pasted code to few lines. i am checking the string for "and " and "or". there may be possibilities of getting these in different forms like " and ","and "," and" etc.
if(operatorTypeused.toLowerCase().indexOf("and")>=0 || operatorTypeused.toLowerCase().indexOf("or")>=0){
if(texttosearch.toLowerCase().indexOf(" and ")>= 0 || texttosearch.toLowerCase().indexOf("and ")>= 0 || texttosearch.toLowerCase().indexOf(" and")>= 0
|| texttosearch.toLowerCase().indexOf(" or ")>= 0 || texttosearch.toLowerCase().indexOf("or ")>= 0 || texttosearch.toLowerCase().indexOf(" or")>= 0){
actualtexttosearch = texttosearch.toLowerCase().replaceAll(" and "," ");
actualtexttosearch = actualtexttosearch.replaceAll(" and","");
actualtexttosearch = actualtexttosearch.replaceAll("and ","");
actualtexttosearch = actualtexttosearch.toLowerCase().replaceAll(" or "," ");
actualtexttosearch = actualtexttosearch.replaceAll(" or","");
actualtexttosearch = actualtexttosearch.replaceAll("or ","");
fieldValue = actualtexttosearch;
}
}
[1822 byte] By [
bobza] at [2007-11-27 5:41:32]

Why aren't you just replacing "and" and "or" without testing if they exist?Kaj
kajbja at 2007-7-12 15:19:09 >

(and you can replace both "and" and "or" with one single line)Why do you want to remove and and or?
kajbja at 2007-7-12 15:19:09 >

there are different conditions here . like: "and", " and", "and ". i can use the word trim. but in case the actual string to search contains "and" at the end or begining of the word (eg:anderson,etc), then it gives problems.
bobza at 2007-7-12 15:19:09 >

> there are different conditions here . like:
>
> "and", " and", "and ". i can use the word trim. but
> in case the actual string to search contains "and"
> at the end or begining of the word
> (eg:anderson,etc), then it gives problems.
Post some sample data and expected result.
The code that you posted will also replace and in anderson.
Kaj
kajbja at 2007-7-12 15:19:09 >

You can use a regular expression:string.matches(".*\\band\\b.*");returns true if and only if the string contains the word "and". It doesn't matter if it's the first word, the last word, or surrounded by comma or period from either side.
search entered by the user: anderson and moneyexpected result: anderson money and should be eliminated. suppose if the user enters search fiels like : "money and", then also it should omit "and" .
bobza at 2007-7-12 15:19:09 >

String data = "andersson or theory money andersson and";System.out.println(data.replaceAll("\\b(?:and|or)\\b", ""));
kajbja at 2007-7-12 15:19:09 >

i will try and let u know
bobza at 2007-7-12 15:19:09 >

String actualtexttosearch = texttosearch.replaceAll("( (or|and)( ))|( (or|and))|((or|and) )","$3");
> String actualtexttosearch =
> texttosearch.replaceAll("( (or|and)( ))|(
> (or|and))|((or|and) )","$3");
That gives:
"andersson theory moneyersson"
on
"andersson or theory money andersson and"
Kaj
kajbja at 2007-7-12 15:19:09 >

I thought we were trying to match the code in the original post. In it, 'and/or' are only replaced with the empty string if they have a leading or trailing space and they are replaced with a single space if there is both a leading space and a trailing space.
Thankyou very much
bobza at 2007-7-12 15:19:09 >

> I thought we were trying to match the code in the
> original post. In it, 'and/or' are only replaced
> with the empty string if they have a leading or
> trailing space and they are replaced with a single
> space if there is both a leading space and a trailing
> space.
I thought that there was a bug in the original code, and it looks like it (read the other replies)
kajbja at 2007-7-12 15:19:09 >

> > I thought we were trying to match the code in the
> > original post. In it, 'and/or' are only replaced
> > with the empty string if they have a leading or
> > trailing space and they are replaced with a single
> > space if there is both a leading space and a
> trailing
> > space.
>
> I thought that there was a bug in the original code,
> and it looks like it (read the other replies)
Reading the OP's responses has made me confused. I now cannot see what he is asking for!
still "or" is coming up in the resultant search field whenever i use or at the end of the string
bobza at 2007-7-21 21:33:13 >

> still "or" is coming up in the resultant search field> whenever i use or at the end of the stringTo my mind you have not specified what you are trying to do!
i am trying to omit "or" and "and" only from the searchable field whenever user enters these.
bobza at 2007-7-21 21:33:13 >

> i am trying to omit "or" and "and" only from the> searchable field whenever user enters these.except when ?
If you want to remove "and" and "or" only when they're used as logical operators, you should only match them when there is whitespace before and after the word: searchString = searchString.replaceAll("(?i)\\s+(and|or)\\s+" " ");
Notice that I replaced the matched text with a single space, because otherwise the search terms would run together. If you really must match the operators at the beginning and end of the search expression, you can do this: searchString = searchString.replaceAll("(?i)(^|\\s+)(and|or)(\\s+|$)" " ");
That will tend to leave spaces at the beginning and end of the search expression, but you can trim() them if that's a problem.
What I don't understand is why you're throwing those operators away. Are you assuming your users are too stupid to use them correctly? Reminds me of the bad old (pre-Google) days, when all the search engines did OR searches by default, and made it difficult if not impossible to do an AND search. "Wow, here I was hoping for just a few useful results, but I got ten thousand useless ones at no extra charge! What a bargain!"
we have a advanced search functionality where different options are avilable. some options should omit the booleans operators.
bobza at 2007-7-21 21:33:13 >
