Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String replace

 
jay Rotti
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Looks simple but need answer. May be it needs some trick which is not getting to this dumb mind.
I have string[] st =new String[2];
st[0]= "Derek Leahy";
st[1]= "Joseph "Tony" Leahy";
st[0].replace(' ','+') gives me Derek+Leahy
where as
st[1].replace(' ','+') give only Joseph+
I need this answer to be Joseph+"Tony"+Leahy;

Can some one help me to treak this trick.
thanks
 
Venkat Avasarala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay,
I am not sure how you get this code to compile. The compiler should stop you at the assignment statement:
st[1]= "Joseph "Tony" Leahy";
But the trick is to negate the special meaning of the double quotes surronding Tony.
So, change the statement to:
st[1] = "Joseph \"Tony\" Leahy";
This should work.
Venkat
Originally posted by jay Rotti:
Hi,
Looks simple but need answer. May be it needs some trick which is not getting to this dumb mind.
I have string[] st =new String[2];
st[0]= "Derek Leahy";
st[1]= "Joseph "Tony" Leahy";
st[0].replace(' ','+') gives me Derek+Leahy
where as
st[1].replace(' ','+') give only Joseph+
I need this answer to be Joseph+"Tony"+Leahy;

Can some one help me to treak this trick.
thanks

 
jay Rotti
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
This value I am getting from database.
while(resultset.next())
{
string[i]=resultset.getString("full_name");
i++;
}
after that in jsp I am displaying the same. Till this stage i don't have any problem.
After this stage I need to replace every occurance of " with + and so on. This where I have problem.
Either I need find index of " or i should replace " with + without "\". Because this is happening dynamically I need to find a way to do this replace dynamically.
any help ?
Thanks

Originally posted by Venkat Avasarala:
Hi Jay,
I am not sure how you get this code to compile. The compiler should stop you at the assignment statement:
st[1]= "Joseph "Tony" Leahy";
But the trick is to negate the special meaning of the double quotes surronding Tony.
So, change the statement to:
st[1] = "Joseph \"Tony\" Leahy";
This should work.
Venkat

 
Venkat Avasarala
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jay,
Here is the deal!
I guess the second result you are getting from the database is not of the form:
st[1] = "first "second" third" - (lets say this type 1)
but of the form:
st[1] = "first " second " third" - (type 2)
So, when the code is executed java sees the space right before the word second and assumes that, thats the end of the string that it needs to read.
If you could somehow manipulate the incoming data as shown in type 1 instead of as in type 2, it should work as you expected it to be.
I know this is not exactly the solution you are looking for, but this is what I could think of right on top of my head. Let me sleep on this and see if I could come up with a better solution.
Venkat
Originally posted by jay Rotti:
Hello,
This value I am getting from database.
while(resultset.next())
{
string[i]=resultset.getString("full_name");
i++;
}
after that in jsp I am displaying the same. Till this stage i don't have any problem.
After this stage I need to replace every occurance of " with + and so on. This where I have problem.
Either I need find index of " or i should replace " with + without "\". Because this is happening dynamically I need to find a way to do this replace dynamically.
any help ?
Thanks


 
jay Rotti
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I think I am going to make one more time my question clear.
St[0]="Username Pass";
st[1]="Pass username uid";
st[2]="Username "uid" password";
Above data I display in JSP and That's fine, Then
st[i].replace(' ','+');
Now If I display my array
St[0]="Username+Pass";
st[1]="Pass+username+uid";
st[2]="Username+";
In st[2] I need to get answer st[2]="Username+"uid"+password"
Please let me know what I can do to get this answer. Please note that I get this data dynamically.
thnaks

Originally posted by Venkat Avasarala:
Jay,
Here is the deal!
I guess the second result you are getting from the database is not of the form:
st[1] = "first "second" third" - (lets say this type 1)
but of the form:
st[1] = "first " second " third" - (type 2)
So, when the code is executed java sees the space right before the word second and assumes that, thats the end of the string that it needs to read.
If you could somehow manipulate the incoming data as shown in type 1 instead of as in type 2, it should work as you expected it to be.
I know this is not exactly the solution you are looking for, but this is what I could think of right on top of my head. Let me sleep on this and see if I could come up with a better solution.
Venkat

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic