FULL IMPLEMENTATION OF THE ORIGINAL STR_PAD
Thanks to theINtoy user answer and I think it would be a good one (not tested yet),
but I ended up implementing the full str_pad php function in Java language, because all answers that I found has partial functionalities and I need more control over string like str_pad php does.
/**************************
*
* STR_PAD IMPLEMENTED
*
**************************/
public String str_pad(String input, int length, String pad, String sense)
{
int resto_pad = length - input.length();
String padded = "";
if (resto_pad <= 0){ return input; }
if(sense.equals("STR_PAD_RIGHT"))
{
padded = input;
padded += _fill_string(pad,resto_pad);
}
else if(sense.equals("STR_PAD_LEFT"))
{
padded = _fill_string(pad, resto_pad);
padded += input;
}
else // STR_PAD_BOTH
{
int pad_left = (int) Math.ceil(resto_pad/2);
int pad_right = resto_pad - pad_left;
padded = _fill_string(pad, pad_left);
padded += input;
padded += _fill_string(pad, pad_right);
}
return padded;
}
protected String _fill_string(String pad, int resto )
{
boolean first = true;
String padded = "";
if (resto >= pad.length())
{
for (int i = resto; i >= 0; i = i - pad.length())
{
if (i >= pad.length())
{
if (first){ padded = pad; } else { padded += pad; }
}
else
{
if (first){ padded = pad.substring(0, i); } else { padded += pad.substring(0, i); }
}
first = false;
}
}
else
{
padded = pad.substring(0,resto);
}
return padded;
}
USAGE - All possible test cases
============================
System.out.println(str_pad("coca cola 100", 10, "-", "STR_PAD_RIGHT"));
System.out.println(str_pad("coca cola", 10, "-", "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 10, "-", "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 10, "-", "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 10, "-", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 15, "##", "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 15, "##", "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 15, "##", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_RIGHT"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_LEFT"));
System.out.println(str_pad("cocacola", 15, "&&&", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 20, "***", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola", 20, "*-*", "STR_PAD_BOTH"));
System.out.println(str_pad("cocacola_light_taste_is_not_like_original", 20, ":::", "STR_PAD_BOTH"));
The first and last println will print all characters like do the original php str_pad function
Output:
System.out: coca cola 100
System.out: coca cola-
System.out: cocacola--
System.out: --cocacola
System.out: -cocacola-
System.out: cocacola#######
System.out: #######cocacola
System.out: ###cocacola####
System.out: cocacola&&&&&&&
System.out: &&&&&&&cocacola
System.out: &&&cocacola&&&&
System.out: ******cocacola******
System.out: *-**-*cocacola*-**-*
System.out: cocacola_light_taste_is_not_like_original
If someone find any good test or improvement, You are welcome to edit this answer for a better code...