Java Char to String charToString(char ch)

Here you can find the source of charToString(char ch)

Description

char To String

License

LGPL

Declaration

public static String charToString(char ch) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String charToString(char ch) {
        if (isAsciiPrintable(ch)) {
            return String.valueOf(ch);
        } else {//from   w w  w  .ja va2  s. c  o  m
            StringBuilder buffer = new StringBuilder("\\u");
            String hex = Integer.toHexString((int) ch);
            for (int i = 0; i < 4 - hex.length(); i++) {
                buffer.append('0');
            }
            buffer.append(hex);
            return buffer.toString();
        }
    }

    public static boolean isAsciiPrintable(char ch) {
        return ch >= 32 && ch < 127;
    }
}

Related

  1. charToString(char c)
  2. charToString(char[] values)
  3. charToString(final char value)