Java examples for java.lang:String Unicode
Returns the ISO_8859_1 bytes of the given string.
//package com.java2s; public class Main { public static void main(String[] argv) { String string = "java2s.com"; System.out.println(java.util.Arrays.toString(toISOBytes(string))); }// w w w .j a va 2s .co m /** ISO-8859-1 */ public static final String ISO_8859_1 = "ISO-8859-1"; /** * Returns the ISO_8859_1 bytes of the given <code>string</code>. * * @param bytes * @return */ public static byte[] toISOBytes(String string) { return toBytes(string, ISO_8859_1); } /** * Returns the string representation of the specified <code>bytes</code> * with the specified <code>charsetName</code> String. * * @param bytes * @param charsetName * @return */ public static String toString(byte[] bytes, String charsetName) { String bytesAsString = null; if (!isNullOrEmpty(bytes)) { try { if (isNullOrEmpty(charsetName)) { bytesAsString = new String(bytes); } else { bytesAsString = new String(bytes, charsetName); } } catch (Exception ex) { bytesAsString = (isNull(bytes) ? null : bytes.toString()); } } return bytesAsString; } /** * Returns the string representation of the specified <code>bytes</code>; * * @param bytes * @return */ public static String toString(byte[] bytes) { return toString(bytes, null); } /** * Converts the specified <code>string</code> into bytes using the specified * <code>charsetName</code>. * * @param string * @param charsetName * @return */ public static byte[] toBytes(String string, String charsetName) { byte[] stringAsBytes = null; if (!isNull(string)) { try { stringAsBytes = isNullOrEmpty(charsetName) ? string .getBytes() : string.getBytes(charsetName); } catch (Exception ex) { ex.printStackTrace(); } } return stringAsBytes; } /** * Converts the specified <code>string</code> into bytes. * * @param string * @return */ public static byte[] toBytes(String string) { return toBytes(string, null); } /** * Returns true if either the string is null or empty otherwise false. * * @param string * @return */ public static boolean isNullOrEmpty(String string) { return (isNull(string) || string.isEmpty()); } /** * Returns true if either the strings array is null or empty otherwise * false. * * @param strings * @return */ public static boolean isNullOrEmpty(Object... objects) { return (isNull(objects) || objects.length == 0); } /** * Returns true if the object is null or otherwise false. * * @param object * @return */ public static boolean isNull(Object object) { return (null == object); } }