How to represent empty char in Java Character class

后端 未结 16 907
遥遥无期
遥遥无期 2020-11-29 02:11

I want to represent an empty character in Java as \"\" in String...

Like that char ch = an empty character;

Actually I want to rep

16条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-29 02:47

    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);
    

提交回复
热议问题