-3
str1.toLowerCase().contains(str2.toLowerCase())

works except it returns true for "11 contains 1" but I want it to search for 1 and not 11 or 213 or any occurrence of 1 but just 1 alone

Vine
  • 46
  • 9

2 Answers2

0

You can loop through your string and do something like this:

for (int i = 0; i < str.length; i++)
{
   if (str[i] == 'your character')
   // your code ...
}
Mohammad Usman
  • 37,952
  • 20
  • 92
  • 95
Amity
  • 1
0
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MyClass {
    public static void main(String args[]) {
        String s = "fsdhfh11sdfsjad";
        Pattern patternOne = Pattern.compile("(.)*(1)(.)*");
        Pattern patternOneOne = Pattern.compile("(.)*(11)(.)*");

        Matcher matchOne = patternOne.matcher(s);
        Matcher matchOneOne = patternOneOne.matcher(s);

        if(matchOne.find() && !matchOneOne.find()){
            System.out.println("Yes");
        }

    }
}

You can test your String with regex like these. This works for me. I hope it could help you. :)

Greethings

Noixes

Noixes
  • 1,158
  • 12
  • 25