Java 使用StringBuilder在字符串中填充空格或其他字符
我是java初学者,这是我关于Stackoverflow的第一篇文章。虽然我的初始代码与本文中的其他文章类似,但我的问题与实现StringBuilder有关,因此这篇文章的原因也是如此 我已经创建了一个方法Java 使用StringBuilder在字符串中填充空格或其他字符,java,string,padding,stringbuilder,Java,String,Padding,Stringbuilder,我是java初学者,这是我关于Stackoverflow的第一篇文章。虽然我的初始代码与本文中的其他文章类似,但我的问题与实现StringBuilder有关,因此这篇文章的原因也是如此 我已经创建了一个方法 **Initial code** private static String padAgain(String result,int padNum) { String str = ""; for(int i = padNum; i &
**Initial code**
private static String padAgain(String result,int padNum)
{
String str = "";
for(int i = padNum; i >= 0; i--)
{
str = String.format("%" + padNum + "s", result).replace(" ", "*");
}
return str;
}
要使用string.format方法在字符串中填充空格(或特殊字符),请执行以下操作。我在我的程序中使用同样的方法来正确地证明数字。数字以2(单个数字)开始,以2的倍数增加(包含n个数字),空格与数字的增加成反比
以下是用于说明目的的输出:
Enter exponent value for base-2 - Enter 0 to exit:
128
Square 1: **************************************2 items
Square 2: **************************************4 items
Square 3: **************************************8 items
Square 4: *************************************16 items
Square 5: *************************************32 items
Square 6: *************************************64 items
Square 7: ************************************128 items
Square 8: ************************************256 items
Square 9: ************************************512 items
Square 10: ***********************************1024 items
Square 11: ***********************************2048 items
Square 12: ***********************************4096 items
Square 13: ***********************************8192 items
Square 14: **********************************16384 items
Square 15: **********************************32768 items
Square 16: **********************************65536 items
Square 17: *********************************131072 items
Square 18: *********************************262144 items
Square 19: *********************************524288 items
Square 20: ********************************1048576 items
Square 21: ********************************2097152 items
Square 22: ********************************4194304 items
Square 23: ********************************8388608 items
Square 24: *******************************16777216 items
Square 25: *******************************33554432 items
Square 26: *******************************67108864 items
Square 27: ******************************134217728 items
Square 28: ******************************268435456 items
Square 29: ******************************536870912 items
Square 30: *****************************1073741824 items
Square 31: *****************************2147483648 items
Square 32: *****************************4294967296 items
Square 33: *****************************8589934592 items
Square 34: ****************************17179869184 items
Square 35: ****************************34359738368 items
Square 36: ****************************68719476736 items
Square 37: ***************************137438953472 items
Square 38: ***************************274877906944 items
Square 39: ***************************549755813888 items
Square 40: **************************1099511627776 items
Square 41: **************************2199023255552 items
Square 42: **************************4398046511104 items
Square 43: **************************8796093022208 items
Square 44: *************************17592186044416 items
Square 45: *************************35184372088832 items
Square 46: *************************70368744177664 items
Square 47: ************************140737488355328 items
Square 48: ************************281474976710656 items
Square 49: ************************562949953421312 items
Square 50: ***********************1125899906842624 items
Square 51: ***********************2251799813685248 items
Square 52: ***********************4503599627370496 items
Square 53: ***********************9007199254740992 items
Square 54: **********************18014398509481984 items
Square 55: **********************36028797018963968 items
Square 56: **********************72057594037927936 items
Square 57: *********************144115188075855872 items
Square 58: *********************288230376151711744 items
Square 59: *********************576460752303423488 items
Square 60: ********************1152921504606846976 items
Square 61: ********************2305843009213693952 items
Square 62: ********************4611686018427387904 items
Square 63: ********************9223372036854775808 items
Square 64: *******************18446744073709551616 items
Square 65: *******************36893488147419103232 items
Square 66: *******************73786976294838206464 items
Square 67: ******************147573952589676412928 items
Square 68: ******************295147905179352825856 items
Square 69: ******************590295810358705651712 items
Square 70: *****************1180591620717411303424 items
Square 71: *****************2361183241434822606848 items
Square 72: *****************4722366482869645213696 items
Square 73: *****************9444732965739290427392 items
Square 74: ****************18889465931478580854784 items
Square 75: ****************37778931862957161709568 items
Square 76: ****************75557863725914323419136 items
Square 77: ***************151115727451828646838272 items
Square 78: ***************302231454903657293676544 items
Square 79: ***************604462909807314587353088 items
Square 80: **************1208925819614629174706176 items
Square 81: **************2417851639229258349412352 items
Square 82: **************4835703278458516698824704 items
Square 83: **************9671406556917033397649408 items
Square 84: *************19342813113834066795298816 items
Square 85: *************38685626227668133590597632 items
Square 86: *************77371252455336267181195264 items
Square 87: ************154742504910672534362390528 items
Square 88: ************309485009821345068724781056 items
Square 89: ************618970019642690137449562112 items
Square 90: ***********1237940039285380274899124224 items
Square 91: ***********2475880078570760549798248448 items
Square 92: ***********4951760157141521099596496896 items
Square 93: ***********9903520314283042199192993792 items
Square 94: **********19807040628566084398385987584 items
Square 95: **********39614081257132168796771975168 items
Square 96: **********79228162514264337593543950336 items
Square 97: *********158456325028528675187087900672 items
Square 98: *********316912650057057350374175801344 items
Square 99: *********633825300114114700748351602688 items
Square 100: ********1267650600228229401496703205376 items
Square 101: ********2535301200456458802993406410752 items
Square 102: ********5070602400912917605986812821504 items
Square 103: *******10141204801825835211973625643008 items
Square 104: *******20282409603651670423947251286016 items
Square 105: *******40564819207303340847894502572032 items
Square 106: *******81129638414606681695789005144064 items
Square 107: ******162259276829213363391578010288128 items
Square 108: ******324518553658426726783156020576256 items
Square 109: ******649037107316853453566312041152512 items
Square 110: *****1298074214633706907132624082305024 items
Square 111: *****2596148429267413814265248164610048 items
Square 112: *****5192296858534827628530496329220096 items
Square 113: ****10384593717069655257060992658440192 items
Square 114: ****20769187434139310514121985316880384 items
Square 115: ****41538374868278621028243970633760768 items
Square 116: ****83076749736557242056487941267521536 items
Square 117: ***166153499473114484112975882535043072 items
Square 118: ***332306998946228968225951765070086144 items
Square 119: ***664613997892457936451903530140172288 items
Square 120: **1329227995784915872903807060280344576 items
Square 121: **2658455991569831745807614120560689152 items
Square 122: **5316911983139663491615228241121378304 items
Square 123: *10633823966279326983230456482242756608 items
Square 124: *21267647932558653966460912964485513216 items
Square 125: *42535295865117307932921825928971026432 items
Square 126: *85070591730234615865843651857942052864 items
Square 127: 170141183460469231731687303715884105728 items
Square 128: 340282366920938463463374607431768211456 items
Enter exponent value for base-2 - Enter 0 to exit:
0
当迭代次数呈指数级增加时,例如从2^10增加到2^128,则String.format()似乎有点慢因此,我用StringBuilder替换了String.format,只获得了部分成功。
[sidebar]为了简洁起见,我没有发布其余的程序代码,而且我的重点是显示的padreach(String,int)方法。如果需要,将发布整个代码。[/sidebar]
**Initial code modified**
private static String padAgain(String result,int padNum)
{
StringBuilder sb = new StringBuilder(result);
for(int i = 1; i < padNum; i++)
{
sb.insert(0, result).insert(sb.indexOf(result), " ").setLength(padNum - result.toCharArray().length);
}
sb.append(result);
return sb.toString();
}
代码解释(我试图做的事情):
- 变量“padNum”存储指数值的最大长度, 最大长度为5的65536
- 变量“result”是以开头的数字的字符串表示形式 二,
- 循环迭代直到达到最大长度
- StringBuilder对象在索引0处插入字符串值。在内部 为此,通过将“result”作为子字符串传递来调用indexOf(字符串) 并在最大长度的0位置插入空格- 每个数字的长度
- 最后附加'result'以弥补最大长度和
- 作为字符串值返回给调用对象
非常感谢论坛专家/成员能够帮助扩展/进一步修改/更正我的StringBuilder代码。如何简化
- padnum是字符串的最大长度
- result.length是已使用的长度
- 因此,您可以通过从整体大小中减去对齐的字符串来计算需要多少占位符将字符串对齐
StringBuilder sb = new StringBuilder(); int rest = padnum - result.length(); for(int i = 1; i < rest; i++) { sb.append(" "); } sb.append(result); return sb.toString();
StringBuilder sb=新建StringBuilder(); int rest=padnum-result.length(); for(int i=1;i
最后,它会添加您的结果。科斯特罗的方法非常棒,但只是针对一点点不同的人
public String pad(String value, int length) {
return pad(value, length, " ");
}
public String pad(String value, int length, String with) {
StringBuilder result = new StringBuilder(length);
result.append(value);
while (result.length() < length) {
result.insert(0, with);
}
return result.toString();
}
其好处是您可以获得一些可用于构建不同解决方案的附加方法
例如,如果我们包括:
public String fill(String value, int length, String with) {
StringBuilder result = new StringBuilder(length);
result.append(value);
result.append(fill(Math.max(0, length - value.length()), with));
return result.toString();
}
然后执行
System.out.println("PAD: " + pad("Testing", 12, "*"));
System.out.println("PAD: " + pad("1", 12, "*"));
System.out.println("FILL: " + fill("1", 12, "*"));
System.out.println("FILL: " + fill("Testing", 12, "*"));
我们得到
PAD: *****Testing
PAD: ***********1
FILL: 1***********
FILL: Testing*****
多亏了科斯特罗纳,这是一项很好的运动,让我多想想自己的想法;) 您还可以使用
数组的fill()
方法来实现更简单的功能
StringBuilder sb = new StringBuilder();
char[] pad = new char[padnum - result.length()];
Arrays.fill(pad, '*');
return sb.append(pad).append(result).toString();
我们现在的时间非常少,但是这种方法比for循环快25%。这可以通过Java 11大大简化,并且它在内部使用数组copoy,字符串concatation也应该在内部转换为stringbuilder
public static String padNum(int number, String pad, int length){
String numString = String.valueOf(number);
int padLength = length - numString.length();
if(padLength > 0)
return pad.repeat(padLength) + numString;
else
return numString;
}
检查rest是否大于0以查看是否需要焊盘,然后使用String.repeat生成正确大小的焊盘
字符串长度将始终为>=padLength使用
String stars = "*".repeat(i);
sb.append(stars);
从:
重复
公共字符串重复(int count)返回其值为
此字符串的串联重复计数次数
如果此字符串为空或计数为零,则空字符串为
返回
参数:
计数-重复返回的次数:
由该字符串重复计数次数或空字符串(如果该字符串为空或计数为零)组成的字符串抛出:
IllegalArgumentException-如果计数为负数。自:
十一,
问题是什么?你指的是哪部分成功?它似乎与您从样本输出中看到的一样?@Hiery,不是。我的目的是用空格填充编号,并且只对样本输出中的5位和4位编号进行填充。对于3,2和1位数字,它打印的不是空格。JavaBegins293这也是一个好主意!请记住,每次插入新空格时,生成器必须将整个字符串向前移动一个字符。在小范围内不会产生明显的影响,它肯定更具可读性,但不是我最喜欢的解决方案;)哦,同意,这只是对问题的另一种看法;)。查看了StringBuilder
代码后,insert和append几乎是一样的,只是insert需要执行arraycopy
。在开始时设置了StringBuilder
的长度后,应确保不会浪费时间为新文本腾出更多空间,但arraycopy
仍将添加(少量)文本overhead@Kostronor,代码确实简化了:)。从外观上看,我已经不必要地将多次插入调用等复杂化了。非常感谢javabegins293
public static String padNum(int number, String pad, int length){
String numString = String.valueOf(number);
int padLength = length - numString.length();
if(padLength > 0)
return pad.repeat(padLength) + numString;
else
return numString;
}
String stars = "*".repeat(i);
sb.append(stars);