Using String format() method: This method is used to return a formatted string using the given locale, specified format string and arguments.
注意:这个方法只能做左右填充。
进场:
- 获取填充所在的字符串。
- 使用 String.format()方法在字符串的左右两边填充空格,然后使用 String.replace()方法将这些空格替换为给定的字符。
对于左填充,使用 String.format()方法的语法是:
```java
String.format("%[L]s", str).replace(' ', ch);
```
对于右填充,使用 String.format()方法的语法是:
```java
String.format("%-[L]s", str).replace(' ', ch);
```
如果长度“L”小于字符串的初始长度,则相同的字符串不变地返回。
下面是上述方法的实现:
示例:
```java
// Java implementation to pad a String
import java.lang.;
import java.io.;
public class GFG {
// Function to perform left padding
public static String
leftPadding(String input, char ch, int L)
{
String result
= String
// First left pad the string
// with space up to length L
.format("%" + L + "s", input)
// Then replace all the spaces
// with the given character ch
.replace(' ', ch);
// Return the resultant string
return result;
}
// Function to perform right padding
public static String
rightPadding(String input, char ch, int L)
{
String result
= String
// First right pad the string
// with space up to length L
.format("%" + (-L) + "s", input)
// Then replace all the spaces
// with the given character ch
.replace(' ', ch);
// Return the resultant string
return result;
}
// Driver code
public static void main(String[] args)
{
String str = "GeeksForGeeks";
char ch = '-';
int L = 20;
System.out.println(
leftPadding(str, ch, L));
System.out.println(
rightPadding(str, ch, L));
}
}
```
Output:
```java
-------GeeksForGeeks
GeeksForGeeks-------
```
Using Apache Common Lang: Apache Commons Lang package provides the StringUtils class, which contains the leftPad(), center() and rightPad() method to easily left pad, center pad and right pad a String respectively.
注意:该模块必须在运行代码之前安装。因此,这些代码不会在在线编译器上运行。
进场:
- 获取填充所在的字符串。
对于左填充,使用stringtutils . left pad()方法的语法是:
```java
StringUtils.leftPad(str, L, ch);
```
对于中心填充,使用 StringUtils.center() 方法的语法是:
```java
StringUtils.center(str, L, ch);
```
对于右填充,使用 StringUtils.rightPad() 方法的语法是:
```java
StringUtils.rightPad(str, L, ch);
```
如果长度“L”小于字符串的初始长度,则相同的字符串不变地返回。
下面是上述方法的实现:
示例:
```java
// Java implementation to pad a String
import java.lang.;
import java.io.;
public class GFG {
// Function to perform left padding
public static String
leftPadding(String input, char ch, int L)
{
// Left pad the string
String result
= StringUtils.leftPad(str, L, ch);
// Return the resultant string
return result;
}
// Function to perform center padding
public static String
centerPadding(String input, char ch, int L)
{
// Center pad the string
String result
= StringUtils.center(str, L, ch);
// Return the resultant string
return result;
}
// Function to perform right padding
public static String
rightPadding(String input, char ch, int L)
{
// Right pad the string
String result
= StringUtils.rightPad(str, L, ch);
// Return the resultant string
return result;
}
// Driver code
public static void main(String[] args)
{
String str = "GeeksForGeeks";
char ch = '-';
int L = 20;
System.out.println(
leftPadding(str, ch, L));
System.out.println(
centerPadding(str, ch, L));
System.out.println(
rightPadding(str, ch, L));
}
}
```
Output:
```java
-------GeeksForGeeks
---GeeksForGeeks----
GeeksForGeeks-------
```