Check String Contains Another String | Code Factory


Donate : Link

Medium Blog : Link

Applications : 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 music

Using 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s