aspose file tools*
The moose likes Beginning Java and the fly likes New line for a word in a string help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "New line for a word in a string help" Watch "New line for a word in a string help" New topic
Author

New line for a word in a string help

annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
How to move a word in a string down one line ?

Ex. String is " 4718384 California, United States April 2008 "

Want output: 4718384 California, April 2008
United States

If I start a new line then everything after "United States" will be on the new line.
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

Use \n, e.g. 4718384 California,\nUnited States April 2008

But to achieve the output that you want, you need to do some String manipulation.
[ April 15, 2008: Message edited by: Freddy Wong ]

SCJP 5.0, SCWCD 1.4, SCBCD 1.3, SCDJWS 1.4
My Blog
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
Originally posted by Freddy Wong:
Use \n, e.g. 4718384 California,\nUnited States April 2008

But to achieve the output that you want, you need to do some String manipulation.

[ April 15, 2008: Message edited by: Freddy Wong ]



The output to what you said would be
4718384 California,
United States April 2008

What types of manipulation are we talking about?
Freddy Wong
Ranch Hand

Joined: Sep 11, 2006
Posts: 959

That depends on the requirements. If the String is fixed, i.e. "4718384 California, United States April 2008 " and it's fairly easy. Just take out the "United States" from the original String and append it to the original String and make sure you add the new line before appending it. You can use substring(), StringBuilder class, etc.

But if you want to achieve more than just a fixed String, you need to think further whether there's some pattern to identify that String.
[ April 16, 2008: Message edited by: Freddy Wong ]
annie roberts
Greenhorn

Joined: Apr 14, 2008
Posts: 12
I already broke the state and country apart by using substring already by using the delimitor "," .

I tried adding new line after country and it doesn't work.

ex.

System.out.print(state + "\n" + country) but everything after country is on the new line.


Nevermind, I found a way. I used the System.out.println(country) last after everything is printed.
[ April 16, 2008: Message edited by: annie roberts ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: New line for a word in a string help