日期:2014-05-20 浏览次数:20885 次
public class Demo { public static void main(String[] args) { print("hello",10); } public static void print(String str,int length){ String result =""; if(str!=null) { if(str.length() >= length) { result = str.substring(0, length); } else { StringBuffer sb = new StringBuffer(str); for (int i = 0;i<length -str.length(); i++) { sb.append("~");//后面用空格填补你能看到吗? } result = sb.toString(); } System.out.println("结果是:"+result); } else { System.out.println("请输入字符串"); } } }