GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Addind a word in the String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Addind a word in the String" Watch "Addind a word in the String" New topic
Author

Addind a word in the String

Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hello,
Im quering a database for certain users and getting their name. I know how to do this. NOw, i want to be able to add middle name(whichis the same for all of the user based on my query), I don't know how to do that. For example: lets say my query returns " Mala Sharma ". I want to add "singh" in between the name. So it would be Mala singh Sharma. How do i do that?. I can't hard code it. Any ideas of suggestion would be greatly appreicated. Thanks a lot.

Mala
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

This isn't the most elegant solution, but it illustrates a general approach using simple methods in String...


(A more "advanced" approach might use a StringBuffer.)
[ December 02, 2004: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jack Lord
Greenhorn

Joined: Jun 04, 2004
Posts: 11
If you change your strings to StringBuffers then you can use the indexOf(String str) method to find the index of the first space (" "), then create and concatenate substrings to get what you need.
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Thanks for your replies. What if my query is return "My first is mala sharma". and i want to do name after My first like "My first name is mala sharma". How do i do that?. I'm just trying to understand the concept. Thanks a lot for your help
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
Somehow...you are going to have to tell Java where to put in the word...for your 1st question it was easy, at the space. For this one, maybe at a certain number of letters in. But in that case, you would have to know what exactly comes before your insertion point, and it would have to be the same every time. So, without somehow knowing the input, it would be impossible...

LoDown
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
See, string depends on the data i get from the result so i wouldn't know the exact length. If i did, i could have used StringBuffer's insert method. BUt i don't know the length so can't tell the position. Am i making sense here?. Any ideas. Thanks
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
I am not quite sure...is the string you need to edit going to be somewhat the same every time? Or completely different?

If you just need to get the length of a string:


but I am not sure that is what you are wanting.
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hey, i'm quering the database and storing the result ina string. For example: String s = rs.getString(1);

Now, i want to add some to this string. For example if my string has cars like "toyota camry le" or "honda accord v6" so on. I want to be able to add "car" to it. so, i would get "toyota camry car le" or "honda accord car v6".(note: im adding car after the first 2 words, and thats what i want to do ). I knwo ths is a bad example but thats the only thing i can think of right now. THanks for your help. I really appreciate it.

Mala
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
To be able to do something like this, you have to have something constant. So if you are ALWAYS adding it after the 1st two words, then it can be done. Or at a certain number of letters in, or some other way to determine where the location in the string is that you want to insert the word. But if it is highly random, then it will be extremely difficult to program. I am sure there is a better way to do this....but here is some messy code to add word after two other words...

Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hey logan,

First of all, i really appreciate your help. But i ended up doing it with StringTokenizer because i was running out of time. Now, i gotta catch my train so i'll reply back tomorrow with what i did. Thanks a lot.

Mala
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hey Logan,

This is what i did to make it work. As i said before, i used StringTokenizer and StringBuffer. Here goes:

It worked like a charm. Do you this there's a better way of doing it?. Thanks for your help.

Mala
Logan Owen
Greenhorn

Joined: Dec 01, 2004
Posts: 24
Looks good...I don't know if it will affect you at all, but the normal StringTokenizer() constructor will treat tabs, newlines, the carriage-return character, and the form-feed character as well as the space as a delimiter, so if you have any of those in your resultset, it could screw it up. Just something to watch out for. This is just conjecture, but it might be faster to use new StringTokenizer( name , " ") so that the tokenizer only has to check for the space.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Well, it looks like you have this solved, but I'll offer this anyway. If you want to find the second space in a String, you could use something like...

int space1 = str.indexOf(' ');
int space2 = str.indexOf(' ', space+1);

(See API for String method indexOf(char, int).)
Mala Sharma
Ranch Hand

Joined: Oct 29, 2004
Posts: 76
Hey Marc,
Thanks for the input. But, i already solved it with indexOf method as well. So, solved it both ways! . Yeap, had nothing else to do at work today, so just enhanced my knowledge. THanks
 
GeeCON Prague 2014
 
subject: Addind a word in the String