java string contains char
/*
This method returns true if the given string contains
the char n. It is necessary due to the
obsolescence of some compilers, which requires us
to write our own contains method.
*/
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;
}
/*
for two chars:
*/
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;
}
String s1 = "My name is GFG";
// prints true
System.out.println(s1.contains("GFG"));
// prints false
System.out.println(s1.contains("geeks"));
Also in Java:
- java convert string with commas to long
- abstract class java constructor
- android java retrofit offline cache
- java using .indexof to fin a space
- treeset java descending order using comparator
- string array in java methods
- create notification android
- Compare integers java sort
- java stream find specific element
- main class java
- @fortawesome/free-solid-svg-icons
- java string contains at beginning
- how to declare a linked list in java
- java substring
- java stream sort Collator
- java string replace character at position
- Unhandled exception: java.lang.InterruptedException
- displaying an arraylist in java
- java 8 add whole array to jsonarray
- java logical operator
- how to check how many anagrams a word has in java
- array of objects in java
- Java array multidimensional hashcode
- java integer compareto