java补充10位字符串到15位
时间: 2023-07-30 13:07:33 浏览: 74
您可以使用字符串的拼接方法实现在字符串后面补充字符的功能。以下是一种Java实现方式:
```java
public static String addPadding(String str, char padChar, int targetLength) {
if (str == null) {
return null;
}
int currentLength = str.length();
if (currentLength >= targetLength) {
return str;
}
StringBuilder sb = new StringBuilder();
sb.append(str);
for (int i = 0; i < targetLength - currentLength; i++) {
sb.append(padChar);
}
return sb.toString();
}
```
上面的方法接收三个参数:原始字符串`str`、用于补充的字符`padChar`和目标长度`targetLength`。如果原始字符串长度已经大于等于目标长度,则直接返回原始字符串。否则,在原始字符串后面不断拼接补充字符,直到达到目标长度为止。
您可以使用以下方式调用该方法:
```java
String str = "abc";
char padChar = '0';
int targetLength = 15;
String result = addPadding(str, padChar, targetLength);
System.out.println(result); // 输出 "abc00000000000"
```
在上面的示例中,我们将字符串`"abc"`补充到长度为`15`,用字符`'0'`进行补充。
阅读全文
相关推荐


















