Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

New line for a word in a string help

 
annie roberts
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
annie roberts
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic