2
String a = "Hello my name is Sap";

String b = "HelloisSapmyname";

String c = "Sap is my name";

String d = "Sap";



String keyword = "Sap";

when I use .contains() it is true for all strings.

How do it get it true for all cases except b?

Note: Adding a space before keyword will not work if the keyword if the first word. Please Help!

EDIT: Added all the possible conditions

SOLUTION: With help from the other answers I found the solution which satisfies every condition

if (a.contains(" "+key+ " ") || a.startsWith(key+" ") 
    || a.equals(key) || a.endsWith(" "+key))

Will regex be faster than this?

Arahasya
  • 525
  • 3
  • 14

5 Answers5

4

You can split the input String into words, convert it to List and run contains on the List:

Arrays.asList("Hello my name is Sap".split(" ")).contains("Sap")
Eran
  • 387,369
  • 54
  • 702
  • 768
  • Convert a String to an array (using regex), then convert an array into a list and then iterate over all of it once again... This need 3 times as much memory as the original text. – Jaroslaw Pawlak Oct 17 '18 at 09:28
  • @JaroslawPawlak you don't have to optimize memory usage for such small examples. I'd always prefer readability (unless memory becomes a bottleneck). – Eran Oct 17 '18 at 09:32
  • Hi. Your answer works. But can you check my solution? I've edited the question. Please tell me if there is a more efficient way – Arahasya Oct 17 '18 at 10:02
  • @ShubhankarPatil your solution seems correct, but also seems less readable to me. – Eran Oct 17 '18 at 10:07
3

You can use simply Regex. \b use for word boundry.

Pattern p = Pattern.compile("\\bSap\\b");

String a ="Hello my name is Sap";
Matcher m1 = p.matcher(a);
System.out.println( m1.find());// return true

String b = "HellomynameisSap";
Matcher m2=p.matcher(b);
System.out.println( m2.find());//return false

String c = "Hello Sap my name";
Matcher m3=p.matcher(c);
System.out.println( m3.find());//return true

String d = "Sap";
Matcher m4=p.matcher(d);
System.out.println( m4.find());//return true

By default it is case sensitive. if you want case insensitivity then you have to initialize Pattern like this.

Pattern p = Pattern.compile("\\bsap\\b",Pattern.CASE_INSENSITIVE);
Khalid Shah
  • 3,132
  • 3
  • 20
  • 39
0

Try this:

if(a.contains(" "+key+" ") || a.startsWith(key) || a.endsWith(key))
Kishita Variya
  • 810
  • 9
  • 19
  • 1
    You should check for "startsWith" before checking for "contains". I suspect it's going to give you a much better performance in some cases. – tanyehzheng Oct 17 '18 at 09:30
  • performance wise startsWith is faster than contains, but there will be lesser cases of startsWith being true than contains, so that won't it create an overhead in cases where contains is true? @tanyehzheng – Kishita Variya Oct 17 '18 at 09:33
  • I agree with you. But given that the overhead is likely to be very small and if "startsWith" happens to be true, it's going to give you a much better performance, I suspect that it's worth it. But of course, without knowing the specific scenario, nobody knows which is faster. – tanyehzheng Oct 17 '18 at 09:37
  • this doesn't work in this case: String "Hello my name is SapHello" – Arahasya Oct 17 '18 at 09:38
0

Simply try the following

if(Str.startsWith("Sap ") ||  Str.contains(" Sap")){
    <code>
}
devdemodemo
  • 11
  • 1
  • 7
  • i think you have updated the question recently. When i posted answer to this there was only 2 possibilities were listed. – devdemodemo Oct 18 '18 at 16:53
0

".contains()" should work.

String a = "Hello my name is Sap";
String b = "HelloisSamynamep";
String c = "Sap is my name";
String d = "Sap";
String keyword = "Sap";

System.out.println(a.contains(keyword));
System.out.println(b.contains(keyword));
System.out.println(c.contains(keyword));
System.out.println(d.contains(keyword));

Output -

true
false
true
true

If i'm not wrong, ".contains()" uses regex internally.

Prateek Paranjpe
  • 513
  • 3
  • 13