Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Addind a word in the String

 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jack Lord
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic