比如:
String str=getString("hello",10);
System.out.println(str);
结果你懂的!
------解决方案--------------------
- Java code
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("请输入字符串"); } }}