Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a Null character in Kotlin

Tags:

java

kotlin

In Java, one can use the escape sequence \0 to represent the Null character

\0 and \0000 are not valid escape sequences in Kotlin, so I've been using a static java char and accessing that from Kotlin.

public class StringUtils{   public static char escapeChar = '\0'; } 

Is there a Null character literal, or a better way of doing this in Kotlin?

like image 411
Caelum Avatar asked Mar 28 '16 19:03

Caelum


People also ask

How do you declare null in Kotlin?

In an effort to rid the world of NullPointerException , variable types in Kotlin don't allow the assignment of null . If you need a variable that can be null, declare it nullable by adding ? at the end of its type. Declares a non- null String variable.

How do you create a null character?

On some keyboards, one can enter a null character by holding down Ctrl and pressing @ (on US layouts just Ctrl + 2 will often work, there is no need for ⇧ Shift to get the @ sign). In documentation, the null character is sometimes represented as a single-em-width symbol containing the letters "NUL".

How do I assign an empty string in Kotlin?

To create an empty String in Kotlin, assign the variable with empty double quotes "" , or with String class constructor String() .

How do you initialize an empty character?

We can use \u0000 value to create an empty char in Java.


2 Answers

These options will work:

  • '\u0000' (Unicode escape syntax, as described in the docs)
  • 0.toChar() (conversions are optimized and have no function call overhead)
  • import java.lang.Character.MIN_VALUE as nullChar, then use nullChar (renaming import)
like image 156
hotkey Avatar answered Sep 19 '22 13:09

hotkey


I believe it must be '\u0000'. Please visit this page for more information.

like image 31
Michael Avatar answered Sep 22 '22 13:09

Michael