Check String Contains Another String | Code Factory

Code Factory
1 min readApr 24, 2020

Reference Link : Link

Donate : Link

StringContainsString.java

package com.codeFactory;import org.apache.commons.lang3.StringUtils;/**
* @author code.factory
*
*/
public class StringContainsString {
public static void main(String... args) {
String str = "Love is a friendship set to music";
System.out.println("String to search: " + str);System.out.println("\nUsing String.contains():");
System.out.println("str.contains(\"love\"): " + str.contains("love"));
System.out.println("str.contains(\"Love\"): " + str.contains("Love"));
System.out.println("\nUsing String.indexOf() processing:");
System.out.println("(str.indexOf(\"love\") > -1 ? true : false): " + (str.indexOf("love") > -1 ? true : false));
System.out.println("(str.indexOf(\"Love\") > -1 ? true : false): " + (str.indexOf("Love") > -1 ? true : false));
System.out.println("\nUsing StringUtils.contains():");
System.out.println("StringUtils.contains(str, \"love\"): " + StringUtils.contains(str, "love"));
System.out.println("StringUtils.contains(str, \"Love\"): " + StringUtils.contains(str, "Love"));
}
}

Output :

String to search: Love is a friendship set to musicUsing String.contains():
str.contains("love"): false
str.contains("Love"): true
Using String.indexOf() processing:
(str.indexOf("love") > -1 ? true : false): false
(str.indexOf("Love") > -1 ? true : false): true
Using StringUtils.contains():
StringUtils.contains(str, "love"): false
StringUtils.contains(str, "Love"): true

Note : i used commons-lang3-3.1.jar for StringUtils.

--

--