使用StringBuilder将字符串用空格或其他字符填充 [英] Use StringBuilder to pad String with blank spaces or other characters

查看:235
本文介绍了使用StringBuilder将字符串用空格或其他字符填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的初学者,这是我有关Stackoverflow的第一篇文章.尽管我的初始代码与此处的其他帖子相似,但我的问题与实现StringBuilder有关,因此我也提出了此帖子的原因.

I'm a beginner to java and this is my first post on Stackoverflow. Although my initial code is similar to other posts here, my question relates to implementing StringBuilder and so the reason for this post.

我已经创建了一个方法

**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个数字),空格与数字的增加成反比.

to left pad the string with blank spaces (or special characters) using String.format method. I'm using the same in my program to right justify numbers. Numbers start with 2 (single-digit) and increase in multiples of 2 (containing n no. of digits) with the spaces being inversely proportional to the increase in digits.

以下是出于说明目的的输出:

Here is the output for illustrative purposes:

    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,但只获得了部分成功.

When the iterations increase exponentially, say from 2^10 to 2^128, then the String.format() seems to be a tad slow. So, I replaced String.format with StringBuilder and met only with partial success.

[sidebar]为简洁起见,我没有发布程序代码的其余部分,我的重点是所示的padAgain(String,int)方法.如果需要,将发布整个代码.[/sidebar]

[sidebar]I've not posted rest of the program code for brevity and also my focus is on padAgain(String,int) method shown.If required, will post entire code.[/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();
    }

及其输出

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"存储指数值的最大长度, 最高为65536.长度5
  • 变量结果"是以数字开头的字符串表示形式 2
  • for循环迭代到最大.达到长度
  • StringBuilder对象,用于在索引0处插入String值. 为此,通过将结果"作为子字符串传递来调用indexOf(String) 并在位置0处插入空格,最大长度为- 每个数字的长度
  • 最后附加结果"以构成最大值.长度和
  • 作为字符串值返回到调用对象
  • variable 'padNum' stores the maximum length of the exponent value, being 65536 of max. length 5
  • variable 'result' is a String representation of numbers starting with 2
  • for loop to iterate till max. length is reached
  • StringBuilder object to insert the String value at index 0. Within this, invoke the indexOf(String) by passing 'result' as a substring of itself and insert blank space at position 0 upto max.length - length of each number
  • finally append 'result' to make up the max. length and
  • return as String value to calling object

非常感谢论坛专家/成员可以帮助扩展/进一步修改/纠正我的StringBuilder代码.

Much appreciated if the forum experts/members could help expand/further modify/correct my StringBuilder code.

推荐答案

您还可以使用Arraysfill()方法更简单一些.

You can also something really simpler with the fill() method of Arrays.

StringBuilder sb = new StringBuilder();
char[] pad = new char[padnum - result.length()];
Arrays.fill(pad, '*');
return sb.append(pad).append(result).toString();

我们的时间很少,但是这种方法比 for循环方法快25%.

We are in really little times but this method is about 25% faster than the for loop one.

这篇关于使用StringBuilder将字符串用空格或其他字符填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆