使用 StringBuilder 用空格或其他字符填充 String
2022-09-04 19:42:42
我是Java的初学者,这是我在Stackoverflow上的第一篇文章。虽然我的初始代码与这里的其他帖子类似,但我的问题与实现StringBuilder有关,因此这篇文章的原因。
我创建了一个方法
**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,只取得了部分成功。
[侧边栏]为了简洁起见,我还没有发布程序代码的其余部分,而且我的重点是padAgain(String,int)方法。如果需要,将发布整个代码。[/侧边栏]
**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();
}
及其输出
Enter exponent value for base-2 - Enter 0 to exit:
16
Square 1: 2 22 items
Square 2: 4 44 items
Square 3: 8 88 items
Square 4: 1616 items
Square 5: 3232 items
Square 6: 6464 items
Square 7: 1128 items
Square 8: 2256 items
Square 9: 5512 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
Enter exponent value for base-2 - Enter 0 to exit:
0
代码解释(我正在尝试执行的操作):
- 变量 'padNum' 存储指数值的最大长度,即最大长度 5 的 65536
- 变量“result”是从 2 开始的数字的字符串表示形式
- for 循环以迭代,直到达到最大长度
- StringBuilder 对象,用于在索引 0 处插入 String 值。在此中,通过将“result”作为自身的子字符串传递来调用indexOf(String),并在位置0处插入空格,直到max.length - 每个数字的长度
- 最后附加“结果”以组成最大长度和
- 作为字符串值返回到调用对象
如果论坛专家/成员可以帮助扩展/进一步修改/更正我的StringBuilder代码,我们将不胜感激。