Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

line break after 44 characters and 11 space from 45 character  RSS feed

 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am adding line break after 44 characters using this code from here.



I w'd like to ask how can I add %11s padding for characters started from 45?

For example, in this text 96 characters:
Historical information ranging from Great Speeches (in audio) to facts about This Day in History

And I want to get: "-----------" - %11s
-----------Historical information ranging from Great Sp
-----------eeches (in audio) to facts about This Day in History
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not entirely sure what you are trying to achieve because your example shows you have padded the string from the first character as well as the 45th but not the 90th. Please explain clearly what you want to do.

If it is that you want to insert "\n-----------" at every 44th character then you just need to replace the insert string in the example code you gave ie:
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with using a StringBuilder?Using the System class method (Java7+ only) gets you out of using plain \n which is platform‑dependent. You can similarly insert a line end at place 34 using similar arithmetic.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone, Sorry for late reply. Due to brain concussion that I got from snowboarding I couldn't check your answers. @Campbell give me some idea with StringBuilder, but the answer of @Tony is also reliable. Thanks for the answers.
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for getting back to us and sorry to hear you were hurt. Hope you've made a full recovery.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope you are better now after the injury.
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your well wishes. I am almost recovered. Also I have added some changes to StringBuilder suggested by @Campbell and it works perfect!!
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!