Search code examples
escapingkotlinstring-literals

Kotlin - Form feed character - Illegal escape: '\f'


Kotlin does not support the escape "\f" (Form Feed Character). So what is the proper way port "\f" from java to Kotlin?

Java:

String str = "\f"; // OK

Kotlin:

var str = "\f"  // Illegal escape: '\f'

Anyway, that looked like a bug to me because Kotlin and java should work well together.


Solution

  • Use unicode escape \u000C. Kotlin doesn't support the \f escape. It isn't very widely used. - in fact I didn't realize that there is \f in Java until seeing your question.

    I made a table on the Java and kotlin escape sequence:

    Escape type|kotlin |java
    \uXXXX      yes     yes
    \XXX        no      yes         // this is Java octal escape.
    \t          yes     yes
    \b          yes     yes
    \n          yes     yes
    \r          yes     yes
    \f          no      yes
    \'          yes     yes
    \"          yes     yes
    \\          yes     yes
    \$          yes     no          // Java just uses $
    

    (Kotlin needs the escaped $ because string templates use $.)