Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to represent empty char in Java Character class

Tags:

java

People also ask

How do you declare an empty character?

You can use c[i]= '\0' or simply c[i] = (char) 0 . The null/empty char is simply a value of zero, but can also be represented as a character with an escaped zero.

Can a char be null in Java?

There's no such entity as NULL in Java. There is null , but that is not a valid value for a char variable. A char can have a value of zero, but that has no particular significance.

How do you use an empty character?

You may assign '\u0000' (or 0). For this purpose, use Character. MIN_VALUE . A null char literal is still a character and takes space in storage.

How do you replace a character with an empty character in Java?

The replace() method looks in the given string for a particular character and returns a new string where the specified character is replaced. For replacedText , the replace() method replaces 'a' char from the text with an empty char. The new string is shown in the output.


You may assign '\u0000' (or 0). For this purpose, use Character.MIN_VALUE.

Character ch = Character.MIN_VALUE;

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.


As Character is a class deriving from Object, you can assign null as "instance":

Character myChar = null;

Problem solved ;)


An empty String is a wrapper on a char[] with no elements. You can have an empty char[]. But you cannot have an "empty" char. Like other primitives, a char has to have a value.

You say you want to "replace a character without leaving a space".

If you are dealing with a char[], then you would create a new char[] with that element removed.

If you are dealing with a String, then you would create a new String (String is immutable) with the character removed.

Here are some samples of how you could remove a char:

public static void main(String[] args) throws Exception {

    String s = "abcdefg";
    int index = s.indexOf('d');

    // delete a char from a char[]
    char[] array = s.toCharArray();
    char[] tmp = new char[array.length-1];
    System.arraycopy(array, 0, tmp, 0, index);
    System.arraycopy(array, index+1, tmp, index, tmp.length-index);
    System.err.println(new String(tmp));

    // delete a char from a String using replace
    String s1 = s.replace("d", "");
    System.err.println(s1);

    // delete a char from a String using StringBuilder
    StringBuilder sb = new StringBuilder(s);
    sb.deleteCharAt(index);
    s1 = sb.toString();
    System.err.println(s1);

}

As chars can be represented as Integers (ASCII-Codes), you can simply write:

char c = 0;

The 0 in ASCII-Code is null.


If you want to replace a character in a String without leaving any empty space then you can achieve this by using StringBuilder. String is immutable object in java,you can not modify it.

String str = "Hello";
StringBuilder sb = new StringBuilder(str);
sb.deleteCharAt(1); // to replace e character

I was looking for this. Simply set the char c = 0; and it works perfectly. Try it.

For example, if you are trying to remove duplicate characters from a String , one way would be to convert the string to char array and store in a hashset of characters which would automatically prevent duplicates.

Another way, however, will be to convert the string to a char array, use two for-loops and compare each character with the rest of the string/char array (a Big O on N^2 activity), then for each duplicate found just set that char to 0..

...and use new String(char[]) to convert the resulting char array to string and then sysout to print (this is all java btw). you will observe all chars set to zero are simply not there and all duplicates are gone. long post, but just wanted to give you an example.

so yes set char c = 0; or if for char array, set cArray[i]=0 for that specific duplicate character and you will have removed it.