find if a character is present or not in a string in java code example
Example 1: contains example in java
String s1 = "My name is GFG";
System.out.println(s1.contains("GFG"));
System.out.println(s1.contains("geeks"));
Example 2: java string contains char
public static boolean contains_char (String main, char secondary)
{
boolean contains_result = false;
for (int i = 0 ; i < input.length () ; i++)
{
if (input.charAt (i) == secondary)
{
contains_result = true;
break;
}
}
return contains_result;
}
public static boolean contains_dualchar (String main, String secondary)
{
boolean contains_result = false;
for (int i = 0 ; i < input.length () ; i++)
{
if (input.charAt (i) == secondary.charAt (0))
{
if (input.charAt (i + 1) == secondary.charAt (1))
{
contains_result = true;
break;
}
}
}
return contains_result;
}