/** * Constructs a String of the fixed length given. If the string passed in is greater than length, * then the string is truncated to length. If the string is greater than length then the string is * padded with spaces. If s is empty or null, then an empty string of spaces is returned. * * @param s - string to operate on * @param length - desired length * @return - fixed length String */ public static String createFixedLengthString(String s, int length) { String result = null; if (StringUtil.isEmpty(s)) { StringBuffer sb = new StringBuffer(length); for (int i = 0; i < length; i++) { sb.append(" "); } result = sb.toString(); } else { if (s.length() == length) { result = s; } else if (s.length() > length) { result = s.substring(0, length); } else { // s.length() is < length StringBuffer sb = new StringBuffer(length); sb.append(s); for (int i = s.length(); i < length; i++) { sb.append(" "); } result = sb.toString(); } } return result; }
public static void main(String args[]) { byte[] testCommand = {0x2, 0x35, 0x35, 0x35, 0x35, 0x20, 0x36, 0x36, 0x36, 0x36, 0xA}; Vector commands = StringUtil.parseCommand(testCommand); System.out.println("Vector size=" + commands.size()); }