Java examples for Internationalization:Chinese
right Pad Chinese To Byte Length
//package com.java2s; import java.io.UnsupportedEncodingException; public class Main { public static String rightPad4ChineseToByteLength(String srcStr, int totalByteLength, String padStr) { if (srcStr == null) { return null; }// www . j a v a 2s . co m int srcByteLength = srcStr.getBytes().length; if (padStr == null || "".equals(padStr)) { padStr = " "; } else if (padStr.getBytes().length > 1 || totalByteLength <= 0) { throw new RuntimeException(""); } StringBuilder rtnStrBuilder = new StringBuilder(); if (totalByteLength >= srcByteLength) { rtnStrBuilder.append(srcStr); for (int i = 0; i < totalByteLength - srcByteLength; i++) { rtnStrBuilder.append(padStr); } } else { byte[] rtnBytes = new byte[totalByteLength]; try { System.arraycopy(srcStr.getBytes("GBK"), 0, rtnBytes, 0, totalByteLength); rtnStrBuilder.append(new String(rtnBytes, "GBK")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return rtnStrBuilder.toString(); } }