Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Line break

 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am reading from an XML file. I want to break the String at particular point.
For example:


In the above code i want to break the line at "All".

How can this be done?

Thanks in advance,
Seema
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Seema,

break?

- You can find punctuation mark (.) in your string and insert '\n' (new line character) after it.

Are you looking for this?
 
Campbell Ritchie
Sheriff
Posts: 48404
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this lot:
  • Put it in a StringBuilder.
  • Find the index of "All".
  • Insert a '\n' character at that index, or better the line.separator String, which you can get from the System.getProperties and System.getProperty methods.
  •  
    Norm Radder
    Ranch Hand
    Posts: 728
    4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    What does break mean?
    As above so that when the line is displayed/printed the string starting with All is on a new line
    OR - break the string into 2 parts, part 1 ending before All and part 2 starting with All. For that you'd use the substring() method to create the two new strings, using the index of where All was found in the original string as the end of string 1 and beginning of string 2.
     
    Seema Sharma
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Campbell & others for your suggestion. Code is working.

    Thanks,
    Seema
     
    Vikas Kapoor
    Ranch Hand
    Posts: 1374
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hey Seema,

    It's great that your code is working.

    But can you show us that what did you want and how did you get it? As me and i guess Norm didn't get what actually you wanted?
     
    Seema Sharma
    Ranch Hand
    Posts: 78
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vishal,
    What you understood was right. I wanted to insert new line charatcter(\n) before "All"(as in example above) so that second line starts in a new line.
    This is how it was done.

     
    Peter Chase
    Ranch Hand
    Posts: 1970
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Actually, although \n will usually produce a line-break, it is not correct portable Java. The reason is that \n is a specific single character, not the platform-dependent line-break, which may not even be a single character. For instance, on Windows, a proper line-break is \r\n.

    The correct way to add a line-break to a string is to get the value of the system property "line.separator" and add that to the string, wherever you want a break.

    As it doesn't change, you can just get store property somewhere well-known in your code. Static import can be useful here!

     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic