As Oracle database
functions witch response for set right or left input text with padding character,
This java method allow
users to padding there input text . this if type of formatting method for
String .
1- Left padding : padds
the input String to the left with given character for the specific length
Method : public String rpad(String inputText , int Stringlen , char padding)
Parameters
inputText : the
input text need to padding
Stringlen : the
length of output String will be .
padding :
the charachter will use to padds the String
Return
String Type : the
new String with padded charachter .
- Right padding : padds
the input String to the right with given character for the specific length
Method : public String rpad(String inputText , int Stringlen , char padding)
Parameters
inputText : the
input text need to padding
Stringlen : the
length of output String will be .
padding :
the charachter will use to padds the String
Return
String Type : the
new String with padded charachter .
package appsstuff.com;
public class StringUtils
{
public String rpad(String inputText , int
Stringlen , char padding){
StringBuilder stringBuilder = new
StringBuilder(inputText);
if(inputText.equals(null)){
return new String();
}
if (inputText.length() >=
Stringlen){
return inputText;
}
int paddingSpace= Stringlen - inputText.length();
for (int i = 0 ; i <paddingSpace
; i++ ){
stringBuilder.append(padding);
}
return stringBuilder.toString();
}
public String rpad(String inputText , int
Stringlen , String padding){
StringBuilder stringBuilder = new
StringBuilder(inputText);
if(inputText.equals(null)){
return inputText;
}
if (inputText.length() >=
Stringlen){
return inputText;
}
int paddingSpace= Stringlen - inputText.length();
for (int i = 0 ; i <paddingSpace
; i++ ){
stringBuilder.append(padding);
}
return stringBuilder.toString();
}
public String lpad(String inputText , int
Stringlen , char padding){
StringBuilder stringBuilder = new
StringBuilder();
if(inputText.equals(null)){
return inputText;
}
if (inputText.length() >= Stringlen){
return inputText;
}
int paddingSpace= Stringlen - inputText.length();
for (int i = 0 ; i <paddingSpace
; i++ ){
stringBuilder.append(padding);
}
stringBuilder.append(inputText);
return stringBuilder.toString();
}
public String lpad(String inputText , int
Stringlen , String padding){
StringBuilder stringBuilder = new
StringBuilder();
if(inputText.equals(null)){
return inputText;
}
if (inputText.length() >=
Stringlen){
return inputText;
}
int paddingSpace= Stringlen - inputText.length();
for (int i = 0 ; i <paddingSpace
; i++ ){
stringBuilder.append(padding);
}
stringBuilder.append(inputText);
return stringBuilder.toString();
}
}
Example
if you need to right
padding input String with character * until length 10 .
assume input String
inputS= "1255"
String res = stringUtil. rpad(inputS , 10 , '*') ;
system.out.printLn(res);
----------------------------------------------
result will be
1255*******
if you change it to left padding with padding character #
1255*******
if you change it to left padding with padding character #
String res = stringUtil.
lpad(inputS , 10 , '#') ;
system.out.printLn(res);
----------------------------------------------
result will be
######1255
######1255