Java Unicode Create toUnicode(String string)

Here you can find the source of toUnicode(String string)

Description

Convert the given string to a string with unicode escape sequence for every character.

License

Open Source License

Parameter

Parameter Description
string The string to be converted.

Return

The string with unicode escape sequence for every character.

Declaration

public static String toUnicode(String string) 

Method Source Code

//package com.java2s;
/*//from w  w w .  j  ava  2s  .  c o  m
 * net/balusc/util/StringUtil.java
 *
 * Copyright (C) 2007 BalusC
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU Lesser General Public License as published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this library.
 * If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Convert the given string to a string with unicode escape sequence for every character.
     * @param string The string to be converted.
     * @return The string with unicode escape sequence for every character.
     */
    public static String toUnicode(String string) {
        StringBuilder builder = new StringBuilder();
        for (char c : string.toCharArray()) {
            builder.append(String.format("\\u%04x", (int) c));
        }
        return builder.toString();
    }
}

Related

  1. toUnicode(String source)
  2. toUnicode(String str)
  3. toUnicode(String str)
  4. toUnicode(String str)
  5. toUnicode(String str)
  6. toUnicode(String strText)
  7. toUnicode(String text)
  8. toUnicode(String text)
  9. toUnicode(String theString, boolean escapeSpace)