str_pad在Java中实现
我见过一些答案,比如str_pad在Java中实现,java,string,Java,String,我见过一些答案,比如 但它们都没有在给定字符串的两侧添加一个pad。 示例: String input = "text"; System.out.println(add_pad(input,10,"#")); 输出:####文本### 我正在寻找的是一种方法,添加一个垫到两侧的字符串 并将字符串和要填充的一个或多个字符作为参数。。。可能是php中的str_pad之类的东西。我怀疑apache commons StringUtils会帮助您: e、 g 有使用java.lang.String
但它们都没有在给定字符串的两侧添加一个pad。
示例:
String input = "text";
System.out.println(add_pad(input,10,"#"));
输出:####文本###
我正在寻找的是一种方法,添加一个垫到两侧的字符串
并将字符串和要填充的一个或多个字符作为参数。。。可能是php中的str_pad之类的东西。我怀疑apache commons StringUtils会帮助您: e、 g
有使用
java.lang.String.format()
和java.util.Formatter
的选项。
基本上:
这里讨论了这些选项:
完全实现原始STRU PAD感谢InToy用户的回答,我认为这将是一个很好的回答(尚未测试),
但我最终用Java语言实现了完整的str_-pad php函数,因为我发现的所有答案都有部分功能,我需要像str_-pad php那样对字符串进行更多的控制
/**************************
*
* 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;
}
第一个和最后一个println将像原始php str_pad函数一样打印所有字符
输出:
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
如果有人发现任何好的测试或改进,欢迎您编辑此答案以获得更好的代码…虽然这可能从理论上回答问题,但请在此处包含答案的基本部分,并提供链接以供参考。谢谢,但出于安全原因,我无法将commons.apache库包含在我的应用程序中。。。
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"));
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