Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes Line break Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Line break " Watch "Line break " New topic

Line break

Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
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,
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hi Seema,


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

Are you looking for this?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38075
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

    Joined: Aug 10, 2005
    Posts: 685
    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

    Joined: May 24, 2007
    Posts: 78
    Thanks Campbell & others for your suggestion. Code is working.

    Vikas Kapoor
    Ranch Hand

    Joined: Aug 16, 2007
    Posts: 1374
    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

    Joined: May 24, 2007
    Posts: 78
    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

    Joined: Oct 30, 2001
    Posts: 1970
    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!

    Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
    I agree. Here's the link:
    subject: Line break
    Similar Threads
    string tokenizer inservlets
    Need Help building Calculator
    Arrays again :-(
    inserting line break in <h:outputText> value
    what is the problem with my java code