I want to represent an empty character in Java as \"\"
in String...
Like that char ch = an empty character;
Actually I want to rep
char
means exactly one character. You can't assign zero characters to this type.
That means that there is no char value for which String.replace(char, char)
would return a string with a diffrent length.
char ch = Character.MIN_VALUE;
The code above will initialize the variable ch
with the minimum value that a char can have (i.e. \u0000
).
As chars
can be represented as Integers (ASCII-Codes), you can simply write:
char c = 0;
The 0 in ASCII-Code is null
.
You can only re-use an existing character. e.g. \0
If you put this in a String, you will have a String with one character in it.
Say you want a char
such that when you do
String s =
char ch = ?
String s2 = s + ch; // there is not char which does this.
assert s.equals(s2);
what you have to do instead is
String s =
char ch = MY_NULL_CHAR;
String s2 = ch == MY_NULL_CHAR ? s : s + ch;
assert s.equals(s2);
As Character is a class deriving from Object, you can assign null as "instance":
Character myChar = null;
Problem solved ;)
this is how I do it.
char[] myEmptyCharArray = "".toCharArray();