Given a string str of some specific length, the task is to pad this string with the given character ch, inorder to make the string of length L.
Note: Padding has to be done in all three formats: Left Padding, Right Padding and Center Padding.
Example:
Input: str = “Geeksforgeeks”, ch =’-‘, L = 20
Output:
Left Padding: ————GeeksForGeeks
Center Padding: ——GeeksForGeeks——
Right Padding: GeeksForGeeks————Input: str = “GfG”, ch =’#’, L = 5
Output:
Left Padding: ##GfG
Center Padding: #GfG#
Right Padding: GfG##
There are many methods to pad a String:
- Using String format() method: This method is used to return a formatted string using the given locale, specified format string and arguments.
Note: This method can be used to do only left and right padding.
Approach:
- Get the string in which padding is done.
- Use the String.format() method to pad the string with spaces on left and right, and then replace these spaces with the given character using String.replace() method.
- For left padding, the syntax to use the String.format() method is:
String.format("%[L]s", str).replace(' ', ch); - For right padding, the syntax to use the String.format() method is:
String.format("%-[L]s", str).replace(' ', ch); - If the length ‘L’ is less than initial length of the string, then the same string is returned unaltered.
Below is the implementation of the above approach:
Example:
// Java implementation to pad a Stringimportjava.lang.*;importjava.io.*;publicclassGFG {// Function to perform left paddingpublicstaticStringleftPadding(String input,charch,intL){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 stringreturnresult;}// Function to perform right paddingpublicstaticStringrightPadding(String input,charch,intL){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 stringreturnresult;}// Driver codepublicstaticvoidmain(String[] args){String str ="GeeksForGeeks";charch ='-';intL =20;System.out.println(leftPadding(str, ch, L));System.out.println(rightPadding(str, ch, L));}}Output:-------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.
Note: This module has to be installed before running of the code. Hence this code won’t run on Online compilers.
Approach:
- Get the string in which padding is done.
- For left padding, the syntax to use the StringUtils.leftPad() method is:
StringUtils.leftPad(str, L, ch);
- For center padding, the syntax to use the StringUtils.center() method is:
StringUtils.center(str, L, ch);
- For right padding, the syntax to use the StringUtils.rightPad() method is:
StringUtils.rightPad(str, L, ch);
- If the length ‘L’ is less than initial length of the string, then the same string is returned unaltered.
Below is the implementation of the above approach:
Example:
// Java implementation to pad a Stringimportjava.lang.*;importjava.io.*;publicclassGFG {// Function to perform left paddingpublicstaticStringleftPadding(String input,charch,intL){// Left pad the stringString result= StringUtils.leftPad(str, L, ch);// Return the resultant stringreturnresult;}// Function to perform center paddingpublicstaticStringcenterPadding(String input,charch,intL){// Center pad the stringString result= StringUtils.center(str, L, ch);// Return the resultant stringreturnresult;}// Function to perform right paddingpublicstaticStringrightPadding(String input,charch,intL){// Right pad the stringString result= StringUtils.rightPad(str, L, ch);// Return the resultant stringreturnresult;}// Driver codepublicstaticvoidmain(String[] args){String str ="GeeksForGeeks";charch ='-';intL =20;System.out.println(leftPadding(str, ch, L));System.out.println(centerPadding(str, ch, L));System.out.println(rightPadding(str, ch, L));}}Output:-------GeeksForGeeks ---GeeksForGeeks---- GeeksForGeeks-------
