You can use string.indexOf('a').
If the char a is present in string :
Answer from mP. on Stack Overflowit returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
You can use string.indexOf('a').
If the char a is present in string :
it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
String.contains()which checks if the string contains a specified sequence of char valuesString.indexOf()which returns the index within the string of the first occurence of the specified character or substring (there are 4 variations of this method)
Java why i cant check if string contains char?
JAVA - String contains char letter. Possible to check? - Stack Overflow
file - How to see if a string contains a specific char in Java? - Stack Overflow
java - How can I find out if a string contanis a char? - Stack Overflow
Hello, i have a array that contains string example: String[]test={“hello”, “bob”} i want to check in a loop if test[x].contains(c); where c is char c = word.chartAt(y); its says that contains in string cannot be applied to char. But why i can do if(test[x].contains(“char”); but not replace char by c since its also a char .. thanks
The problem is that string length is compared to character value:
str.length() == chr
What you should use if the indexOf String method
public boolean contains(String str, char chr) {
return str.indexOf(chr) != -1;
}
You are testing if the length of the String is equal to some char. This will perhaps work if your String is of length zero and your character == 0 or perhaps if your String is of length one and you compare it to the character with the numeric value of 1. In other words: Almost never.
You never return false. So all your string contain all your characters.
String has an indexOf( character ) method. use that. See documentation.
char is not CharSequence
Your call to String::contains takes a CharSequence such as String while you are passing a char. Two different types.
Use the double-quote mark rather than single-quote mark around < to get a String object rather than a char primitive: "<" versus '<'.
boolean containsLessThan = input.contains( "<" ) ;
If your goal is simply deletion of such characters, use String::replace.
String result = input.replace( "<" , "" ).replace( ">" , "") ;
FYI, the char type is obsolete, unable to represent even half of the characters defined in Unicode.
Here is a chart I created showing the various text-related classes bundled with Java. String is one of several classes that implement the CharSequence interface. The char type is not included here because it is (a) a primitive rather than a class, and (b) obsolete.

By the way, recommended reading: The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
You can use info.indexOf('<')
it returns the the index of the first occurrence of the character in the character sequence represented by this object, or -1 if the character does not occur.
So this would be your code: if(info.indexOf('<') > -1)
Strong Recommendation: Please first search enough and/or read the right documentation for each part of the code that you're working with or each time get stuck. Asking question would be the last choice when you absolutely devistated. Here is exactly what you asked for example:
https://stackoverflow.com/questions/506105/how-can-i-check-if-a-single-character-appears-in-a-string#:~:text=You%20can%20use%20string.,the%20character%20does%20not%20occur.
you can do something like thie
if(stringVar.contains(String.valueOf(charValue)))
{
//do somethinng
}
http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#indexOf(int)
Returns the index within this string of the first occurrence of the specified character...
So, given:
String string = "Hello"
string.indexOf('H') == 0
string.indexOf('e') == 1
string.indexOf('l') == 2
string.indexOf('l') == 3
string.indexOf('o') == 4
string.indexOf('n') == -1
If your string doesn't contain the char you're checking for, it will return a value of -1. So to see if your string contains a char, you are looking for:
boolean contains = string.indexOf(char_value) > -1;
Or
boolean contains = string.indexOf(char_value) != -1;
if ( str.indexOf( ch ) != -1 ) should work.
String.contains only accepts a CharSequence, but one Character is not a CharSequence. The way above works for Characters, too. Another way, as other people have posted (but I want to explain a little bit more), would be to make your single Character into a CharSequence, for example by creating a String...
String x = "" + b; // implicit conversion
String y = Character.valueOf(ch).toString(); // explicit conversion
This is because the String has not any overloaded contains() method for char.
Use the String.contains() method for CharSequence like -
String ch = "b";
str.contains(ch);
Her the CharSequence is an interface. A CharSequence is a readable sequence of char values. This interface provides uniform, read-only access to many different kinds of char sequences.
All known implementation of at JDK are: CharSequence are - CharBuffer, Segment, String, StringBuffer, StringBuilder.