Two Laptop Bag
The moose likes Beginning Java and the fly likes Manipulating String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Manipulating String?" Watch "Manipulating String?" New topic

Manipulating String?

George Han

Joined: Aug 17, 2005
Posts: 17
I tried and tried to figure ways but cant, could someone please tell me a good way to read a name as a string, and manipulate it?
I need to do this:
Enter: John, Doe E.
Output: Doe E. John
When you see the ',' you have to put the word in front of it on the back, but I dont even know how to do that with string. Also another bad thing is that I have to read the string all at once. So could someone point me to the right direction?
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 980

Make use of the stringTokenizer and split the original string into two halves...and then join them again in the order of ur choice..

A Kumar
madhup narain
Ranch Hand

Joined: Dec 14, 2004
Posts: 148
I think String.split would be a better option.
split the string
iterate through each element of the collected string elements
look for the ',' in each string
and then perhaps place them in another string


Money for nothing and Java for Free
Selva Varadhan

Joined: Aug 20, 2005
Posts: 14
Hi Pls,
Clarify one thing, r u getting input as a parametter for the program, or by any other source of input like data base. if it is the first case, the space between the word split the data and you won't get the expected input.

If it is the second case use this code as narain told.

String inputString = "John, Doe E";
String outputString[] =null;
outputString = inputString.split(",");
System.out.println(outputString[1]+" "+ outputString[0]);

I thing this will help u.
I agree. Here's the link:
subject: Manipulating String?
It's not a secret anymore!