if (c == ' ')
char is a primitive data type, so it can be compared with ==.
Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' ').
if (c == ' ')
char is a primitive data type, so it can be compared with ==.
Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' ').
The code you needs depends on what you mean by "an empty space".
If you mean the ASCII / Latin-1 / Unicode space character (0x20) aka SP, then:
if (ch == ' ') { // ... }If you mean any of the traditional ASCII whitespace characters (SP, HT, VT, CR, NL), then:
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\x0b') { // ... }If you mean any Unicode whitespace character, then:
if (Character.isWhitespace(ch)) { // ... }
Note that there are Unicode whitespace includes additional ASCII control codes, and some other Unicode characters in higher code planes; see the javadoc for Character.isWhitespace(char).
What you wrote was this:
if (Equals(ch, " ")) {
// ...
}
This is wrong on a number of levels. Firstly, the way that the Java compiler tries to interpret that is as a call to a method with a signature of boolean Equals(char, String).
- This is wrong because no method exists, as the compiler reported in the error message.
Equalswouldn't normally be the name of a method anyway. The Java convention is that method names start with a lower case letter.- Your code (as written) was trying to compare a character and a String, but
charandStringare not comparable and cannot be cast to a common base type.
There is such a thing as a Comparator in Java, but it is an interface not a method, and it is declared like this:
public interface Comparator<T> {
public int compare(T v1, T v2);
}
In other words, the method name is compare (not Equals), it returns an integer (not a boolean), and it compares two values that can be promoted to the type given by the type parameter.
Someone (in a deleted Answer!) said they tried this:
if (c == " ")
That fails for two reasons:
" "is a String literal and not a character literal, and Java does not allow direct comparison ofStringandcharvalues.You should NEVER compare Strings or String literals using
==. The==operator on a reference type compares object identity, not object value. In the case ofStringit is common to have different objects with different identity and the same value. An==test will often give the wrong answer ... from the perspective of what you are trying to do here.
For a non-regular expression approach, you can check Character.isWhitespace for each character.
boolean containsWhitespace(String s) {
for (int i = 0; i < s.length(); ++i) {
if (Character.isWhitespace(s.charAt(i)) {
return true;
}
}
return false;
}
Which are the white spaces in Java?
The documentation specifies what Java considers to be whitespace:
public static boolean isWhitespace(char ch)Determines if the specified character is white space according to Java. A character is a Java whitespace character if and only if it satisfies one of the following criteria:
- It is a Unicode space character (SPACE_SEPARATOR, LINE_SEPARATOR, or PARAGRAPH_SEPARATOR) but is not also a non-breaking space ('\u00A0', '\u2007', '\u202F').
- It is
'\u0009', HORIZONTAL TABULATION.- It is
'\u000A', LINE FEED.- It is
'\u000B', VERTICAL TABULATION.- It is
'\u000C', FORM FEED.- It is
'\u000D', CARRIAGE RETURN.- It is
'\u001C', FILE SEPARATOR.- It is
'\u001D', GROUP SEPARATOR.- It is
'\u001E', RECORD SEPARATOR.- It is
'\u001F', UNIT SEPARATOR.
boolean containsWhitespace = false;
for (int i = 0; i < text.length() && !containsWhitespace; i++) {
if (Character.isWhitespace(text.charAt(i)) {
containsWhitespace = true;
}
}
return containsWhitespace;
or, using Guava,
boolean containsWhitespace = CharMatcher.WHITESPACE.matchesAnyOf(text);
Literally, the answer to your question is:
list[i] = ' ';
However, your code as written looks rather broken. Hint: think carefully about:
what happens to characters that aren't letters in the range
atoz(lower case!), andwhat happens to
letterbefore this test -if (letter == ' ') { ....
A space is a character similar to any other letter:
char space[] = {' '};
or
myCharArray[i] = ' ';