0

I want to find particular string in JAVA and I am using CONTAINS function for that.

But problem with CONTAINS is that it gives true even if there is a super string available for that string.

Ex- let's say

String i = "anand > 5 or id < 6" 

and I want to check whether string contains AND or OR. But here i.contains("and") will give true because of anand.

How to solve this issue?

Is there any function available in library?

2
  • public class java.lang.String extends java.lang.Object { public int substr(String str) { } } Commented Jan 5, 2012 at 10:11
  • just curious, what problem are you trying to solve? Commented Jan 5, 2012 at 10:14

7 Answers 7

5
if (i.matches("(?s).*\\b(and|or)\\b.*")) System.println("AND or OR found");

(?s) let . also match newlines \\b is a word boundary


In answer to comment "hey now I wanted..."

String[] words = ...;
StringBuilder expr = new StringBuilder();
for (String word : words) {
    if (expr.length() != 0)
        expr.append("|"):
    expr.append(word);
}
i = i.replaceAll("\\b(" + expr + ")\\b", "bla");
Sign up to request clarification or add additional context in comments.

2 Comments

can you explain it more in context of my example...??
hey now I wanted to match string stored in an array... If main string contains that string of array then I want to replace it... Can you suggest me for this.
2

You might want to take a look at regular expressions and use the following regex:

"\\band\\b"

The \\b should denote the regex to match whole words only.

Comments

1

You want to check whether your string contains AND or OR then it is wrong to check i.contains("and") you should use contains method with case sensitive. to check whether string contains AND you need to write contains("AND")

String i = "anand > 5 or id < 6";
        System.out.println(i.contains("AND"));//returns false

        System.out.println(i.contains("and"));//returns true

because the string contains and not AND it is case sensitive

1 Comment

@Downvoters, mention your precious reason behind down voting. which will help others to know their mistakes. Dont hide yourself by not mentioning the reason.
0

looks like you want to check for " AND " (with spaces) instead of just "and"

4 Comments

No I want to strictly find whether string contains AND...?? But function will give true even in cases like ANAND,HAND,SAND etc...
What do you mean strictly? If you want it to be so strict, use the equals method! light_303's method of checking for ` AND ` will work and WON'T give true for strings like ANAND.
String test = "sand or nosand"; boolean containsAnd = test.contains(" and ");
Your requirement is not clear : can you post exemples of string that should return True and String that should return false ? For instance is ANDOGAMY a valid string ?
0

I would do like this : - fist upper case the sting you are searching for (to avoid or/OR and/AND issues) - search for " and " (ie with leading and trailing space)

Comments

0

Looks you want regular expressions where you surround the word you are looking for with word breaks, i.e.

"\\bAND\\b"

This will match AND only when it is not surrounded by other letters.

Comments

0

You want whole word search. It could be done with regexps:

i.matches("\\band\\b")

where \b stands for word boundary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.