Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
String all pad .leftPad rightpad and bothPad
package co.dogduck.java.utils.string;
public class MyStrPad {
public enum PAD{LEFT, RIGHT, BOTH}
public static String myStrPad(String input,int padLength){
return myStrPad(input,padLength," ",PAD.RIGHT);
}
public static String myStrPad(String input,int padLength,String padString){
return myStrPad(input,padLength,padString,PAD.RIGHT);
}
public static String myStrPad(String input,int padLength,String padString,PAD pad){
if(padLength <= input.length()) return input;
int length = padLength-input.length();
switch (pad){
case LEFT: return strPad(length,padString)+input;
case RIGHT:return input+strPad(length,padString);
case BOTH: return strPad(length/2,padString)+input+strPad(length-length/2,padString);
default: throw new EnumConstantNotPresentException(PAD.class,"Enum type error !");
}
}
// (8,wi)->wiwiwiwi (7,wi)->wiwiwiw
private static String strPad(int padLength,String padString){
String temp = "";
for(int i=0;i<padLength/padString.length()+1;i++)
temp+=padString;
return temp.substring(0,padLength);
}
}
package co.dogduck.java.utils.string;
import junit.framework.TestCase;
/**
* Created by dog on 4/17/16.
*/
public class Test extends TestCase {
public void test(){
assert MyStrPad.myStrPad("abc",-1,"w").equals("abc");
assert MyStrPad.myStrPad("abc",0,"w").equals("abc");
assert MyStrPad.myStrPad("abc",1,"w").equals("abc");
assert MyStrPad.myStrPad("abc",2,"w").equals("abc");
assert MyStrPad.myStrPad("abc",3,"w").equals("abc");
assert MyStrPad.myStrPad("abc",4,"w").equals("abcw");
assert MyStrPad.myStrPad("abc",5,"w").equals("abcww");
assert MyStrPad.myStrPad("abc",6,"w").equals("abcwww");
assert MyStrPad.myStrPad("abc",-1,"wi").equals("abc");
assert MyStrPad.myStrPad("abc",0,"wi").equals("abc");
assert MyStrPad.myStrPad("abc",1,"wi").equals("abc");
assert MyStrPad.myStrPad("abc",2,"wi").equals("abc");
assert MyStrPad.myStrPad("abc",3,"wi").equals("abc");
assert MyStrPad.myStrPad("abc",4,"wi").equals("abcw");
assert MyStrPad.myStrPad("abc",5,"wi").equals("abcwi");
assert MyStrPad.myStrPad("abc",6,"wi").equals("abcwiw");
assert MyStrPad.myStrPad("abc",7,"wi").equals("abcwiwi");
assert MyStrPad.myStrPad("abc",8,"wi").equals("abcwiwiw");
assert MyStrPad.myStrPad("abc",-1,"w",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",0,"w",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",1,"w",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",2,"w",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",3,"w",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",4,"w",MyStrPad.PAD.RIGHT).equals("abcw");
assert MyStrPad.myStrPad("abc",5,"w",MyStrPad.PAD.RIGHT).equals("abcww");
assert MyStrPad.myStrPad("abc",6,"w",MyStrPad.PAD.RIGHT).equals("abcwww");
assert MyStrPad.myStrPad("abc",-1,"wi",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",0,"wi",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",1,"wi",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",2,"wi",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",3,"wi",MyStrPad.PAD.RIGHT).equals("abc");
assert MyStrPad.myStrPad("abc",4,"wi",MyStrPad.PAD.RIGHT).equals("abcw");
assert MyStrPad.myStrPad("abc",5,"wi",MyStrPad.PAD.RIGHT).equals("abcwi");
assert MyStrPad.myStrPad("abc",6,"wi",MyStrPad.PAD.RIGHT).equals("abcwiw");
assert MyStrPad.myStrPad("abc",7,"wi",MyStrPad.PAD.RIGHT).equals("abcwiwi");
assert MyStrPad.myStrPad("abc",8,"wi",MyStrPad.PAD.RIGHT).equals("abcwiwiw");
assert MyStrPad.myStrPad("abc",-1,"w",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",0,"w",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",1,"w",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",2,"w",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",3,"w",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",4,"w",MyStrPad.PAD.LEFT).equals("wabc");
assert MyStrPad.myStrPad("abc",5,"w",MyStrPad.PAD.LEFT).equals("wwabc");
assert MyStrPad.myStrPad("abc",6,"w",MyStrPad.PAD.LEFT).equals("wwwabc");
assert MyStrPad.myStrPad("abc",-1,"wi",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",0,"wi",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",1,"wi",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",2,"wi",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",3,"wi",MyStrPad.PAD.LEFT).equals("abc");
assert MyStrPad.myStrPad("abc",4,"wi",MyStrPad.PAD.LEFT).equals("wabc");
assert MyStrPad.myStrPad("abc",5,"wi",MyStrPad.PAD.LEFT).equals("wiabc");
assert MyStrPad.myStrPad("abc",6,"wi",MyStrPad.PAD.LEFT).equals("wiwabc");
assert MyStrPad.myStrPad("abc",7,"wi",MyStrPad.PAD.LEFT).equals("wiwiabc");
assert MyStrPad.myStrPad("abc",8,"wi",MyStrPad.PAD.LEFT).equals("wiwiwabc");
assert MyStrPad.myStrPad("abc",-1,"w",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",0,"w",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",1,"w",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",2,"w",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",3,"w",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",4,"w",MyStrPad.PAD.BOTH).equals("abcw");
assert MyStrPad.myStrPad("abc",5,"w",MyStrPad.PAD.BOTH).equals("wabcw");
assert MyStrPad.myStrPad("abc",6,"w",MyStrPad.PAD.BOTH).equals("wabcww");
assert MyStrPad.myStrPad("abc",7,"w",MyStrPad.PAD.BOTH).equals("wwabcww");
assert MyStrPad.myStrPad("abc",-1,"wi",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",0,"wi",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",1,"wi",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",2,"wi",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",3,"wi",MyStrPad.PAD.BOTH).equals("abc");
assert MyStrPad.myStrPad("abc",4,"wi",MyStrPad.PAD.BOTH).equals("abcw");
assert MyStrPad.myStrPad("abc",5,"wi",MyStrPad.PAD.BOTH).equals("wabcw");
assert MyStrPad.myStrPad("abc",6,"wi",MyStrPad.PAD.BOTH).equals("wabcwi");
assert MyStrPad.myStrPad("abc",7,"wi",MyStrPad.PAD.BOTH).equals("wiabcwi");
assert MyStrPad.myStrPad("abc",8,"wi",MyStrPad.PAD.BOTH).equals("wiabcwiw");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment