All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.rekex.common_util.EscapeJavaChar Maven / Gradle / Ivy

The newest version!
package org.rekex.common_util;

/**
 * Escape chars as they would appear in Java source code.
 */
public class EscapeJavaChar
{
    public static void append(StringBuilder sb, String s)
    {
        // note that we are operating on `char` level, not code points
        // a surrogate pair will be escaped into two \\uhhhh 

        for(int i=0; i>12));
        sb.append(hex(ch>>8));
        sb.append(hex(ch>>4));
        sb.append(hex(ch));
    }
    static char hex(int x)
    {
        return "0123456789ABCDEF".charAt(x & 0xF);
    }

    public static String asInJavaSource(String str)
    {
        StringBuilder sb = new StringBuilder();
        appendQ(sb, str);
        return sb.toString();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy