• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

split the string to substrings of equal size

 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have asked about line break after maxLength charachter on my 17 Feb. post.
Now I would like to split the string to substrings of equal size (by considering \\s).
I am using this regex code:

But output in 3rd line it prints with additional "-----------":

I would like to ask how can I print without 3rd line?
Your time and help is greatly appreciated.
 
Greenhorn
Posts: 12
PHP AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this what you expect?
 
Tai Yo
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Trung Hieu Hoang when the length of string is <100 in that case it doesn't break line after 54 character
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tai Yo wrote:I would like to ask how can I print without 3rd line?


OK, well first of all: Don't try cramming everything into one regex.

Programming isn't a competition to see how short you can make your code. In fact, if anything, it's the reverse - especially when you're dealing with regexes.
Programs should be written for other programmers to read, so always favour clarity over brevity.

Reading between the lines of your original post (here, for anyone who's interested), you want to:
1. Split a piece of text into "lines" of 44 characters each (apart from the last one).
2. Print them out, prepending
  • the first line with "TITLE", followed by 6 spaces (making 11 in all).
  • all subsequent ones with a "line" of 11 consecutive hyphens so that all the text lines up.
  • And you're trying to do ALL THAT in one statement.

    Now it's possible it can be done, but it's "regex compression" gone mad.

    On the other hand, the whole thing can be done quite clearly, and much more easily, if you take things step-by-step, viz:
    Remember: Short != Good.

    HIH

    Winston
     
    Tai Yo
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    @Winston thank you for clear explanation. Yes, you are right I tried to make as shorter as possible, but result looks worst.
    I have checked your code and I found that if in this case:

    The output look like this:

    After every 24 character appears additional "--------" and this sentence "A(b,c)D" was divided.
    I would like to ask is it possible split by space, i.e. in this form:
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tai Yo wrote:The output look like this:]


    It shouldn't, because println() should add a newline after each output. Could you show us the exact code you wrote to get this (all of it)?

    Also: if you want to split on word boundaries as well as limiting line output to 24 characters max, the logic is quite a bit more complicated; so let's deal with one thing at a time.

    Winston
     
    Tai Yo
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Also if possible could you explain the meaning of "%-11s%s%n", I didn't understand why you have added this part "%s%n".
     
    Winston Gutkowski
    Bartender
    Posts: 10780
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ah, OK. So you're not using System.out; it looks like you're adding to a String that ends up holding your entire output text.

    Personally, I'd use a PrintStream or PrintWriter so you can use the methods I showed you. Adding to a String is very wasteful.

    Also: I don't think you've posted the exact code, because
    (a) your for loop isn't closed.
    (b) your outfile.write() is in the wrong place.

    Also if possible could you explain the meaning of "%-11s%s%n", I didn't understand why you have added this part "%s%n".


    Well, you already know what "%-11s" does. "%s" is just another string immediately afterwards (and uses the 2nd argument), "%n" is the equivalent of System.lineSeparator().

    HIH

    Winston
     
    Marshal
    Posts: 8863
    637
    Mac OS X VI Editor BSD Java
    • Likes 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Tai Yo wrote:Also if possible could you explain the meaning of "%-11s%s%n", I didn't understand why you have added this part "%s%n".

    That is not part of regular expression, these are format specifiers.

  • first and second % being replaced with arguments "TITLE" and lines[0]
  • %-11s specifies, that for the first formatted string are 11 allocated spaces. - sign in front means, that it is aligned to left
  • %s just specifies next string, which comes from lines[0], starting from position 12 from left (because 11 allocated for the first argument)
  • %n is platform independent new line character

  • All that gives you an output of (numbers I added for visualization to show how that number 11 reflects to the output):
    %n as mentioned above would cause a new line
     
    Tai Yo
    Ranch Hand
    Posts: 98
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Liutauras Vilda wrote:

  • first and second % being replaced with arguments "TITLE" and lines[0]
  • %-11 specifies, that for the first formatted string are 11 allocated spaces. - sign in front means, that it is aligned to left
  • %s just specifies next string, which comes from lines[0], starting from position 12 from left (because 11 allocated for first argument
  • %n is platform independent new line character

  • All that gives you an output of (numbers I added for visualization to show how that number 11 reflects to the output):
    %n as mentioned above would cause a new line


    Thank you for your detailed and clear explanation. It was very helpful and understandable.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic