shragi
asked on
Java string replace
Hi - I am trying to replace a string of length 13 with 40 char string, but i had a small problem.
Example:
String s = "hi this is testuser abcdefghijklmnopqurstu"
Now i want to replace "testuser " 13 char with a 40 char string "XXXhblahblahblahblahblahb lahblahbla hXXX"
so i did like this
string replace = "XXXhblahblahblahblahblahb lahblahbla hXXX"
s = s.replace (12,12+40, replace);
it works but my final sting is like below
"hi this is XXXhblahblahblahblahblahbl ahblahblah XXX"
instead I expected
"hi this is XXXhblahblahblahblahblahbl ahblahblah XXXabcdefg hijklmnopq urstu"
so how can i insert a string also replace an existing string.
Thanks,
Example:
String s = "hi this is testuser abcdefghijklmnopqurstu"
Now i want to replace "testuser " 13 char with a 40 char string "XXXhblahblahblahblahblahb
so i did like this
string replace = "XXXhblahblahblahblahblahb
s = s.replace (12,12+40, replace);
it works but my final sting is like below
"hi this is XXXhblahblahblahblahblahbl
instead I expected
"hi this is XXXhblahblahblahblahblahbl
so how can i insert a string also replace an existing string.
Thanks,
ASKER
Hi Pawan - you are hard coding it, I am trying to replace string fron 12th index.
Thanks,
Thanks,
Hi,
Please try like this ..
s = s.replace (12,12+8, replace);
Please try like this ..
s = s.replace (12,12+8, replace);
ASKER
Hi Pawan - Here in this example I gave the replacement string as "XXXhblahblahblahblahblahb lahblahbla hXXX" but the replacement string doesn't necessarly be 40 all the time
if the replacement string is "XXXhblahblahblahbl"
then my final string should look like
so irrespective of replacement string i want to replace a 13char with 40 char string
if the replacement string is "XXXhblahblahblahbl"
then my final string should look like
"hi this is XXXhblahblahblahbl abcdefghijklmnopqurstu"
so irrespective of replacement string i want to replace a 13char with 40 char string
ASKER
s = s.replace (12,12+8, replace); will work but what if the replace string is not 40 char length and if it is just 34 char length.
If the replace string is just 34 char length then i want to insert remaining with empty spaces.
If the replace string is just 34 char length then i want to insert remaining with empty spaces.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
One of the reasons this question is problematic is that you're not describing your goal - you're really describing some technical steps that you've tried to achieve your goal with and failed. Goals can almost always be, and should be, described in non-technical language.
Towards the end, you seemed to hint that the result should be of a certain length, yet there's no mention of that at the top of the question ..
http://technojeeves.com/index.php/aliasjava1/15-smart-questions
Towards the end, you seemed to hint that the result should be of a certain length, yet there's no mention of that at the top of the question ..
http://technojeeves.com/index.php/aliasjava1/15-smart-questions
Hi Author,
Please try my solution. You should be able to achieve the expected result.
Hope it helps!
Please try my solution. You should be able to achieve the expected result.
Hope it helps!
I agree with CEHJ that a verbal explanation of what you want to accomplish might be better for our understanding of your issue. That being said, and assuming you always want to remove 13 characters and replace them with 40 characters, the following might do what you want.
String s = "hi this is testuser abcdefghijklmnopqurstu";
StringBuilder remove = new StringBuilder("testuser");
//right pad with blanks to be 13 characters then
for (int i = remove.length(); i < 13; i++) {remove.append(" ");}
String removeStr = remove.toString();
StringBuilder replace = new StringBuilder("XXXhblahbla hblahbl");
//right pad with blanks to be 40 characters then
for (int i = replace.length(); i < 40; i++){replace.append(" ");}
String replaceStr = replace.toString();
String returnStr = s.replaceAll(removeStr, replaceStr);
String s = "hi this is testuser abcdefghijklmnopqurstu";
StringBuilder remove = new StringBuilder("testuser");
//right pad with blanks to be 13 characters then
for (int i = remove.length(); i < 13; i++) {remove.append(" ");}
String removeStr = remove.toString();
StringBuilder replace = new StringBuilder("XXXhblahbla
//right pad with blanks to be 40 characters then
for (int i = replace.length(); i < 40; i++){replace.append(" ");}
String replaceStr = replace.toString();
String returnStr = s.replaceAll(removeStr, replaceStr);
I dont prefer loops for this simple stuff. My solution is working fine without any loops.
Pawan,
The problem is you're still hard-coding part of the solution for this one instance. We need to have shragi provide more detail about the requirements intended in all instances. What happens to the solution if the string is "hello this is testuser abcdefghijklmnopqurstu"? In that case the s.replace(12,... would not work. What happens if the replacement string is greater than 40 characters or the string to replace is greater than 13 characters? We need a more definitive description of the requirements.
The problem is you're still hard-coding part of the solution for this one instance. We need to have shragi provide more detail about the requirements intended in all instances. What happens to the solution if the string is "hello this is testuser abcdefghijklmnopqurstu"? In that case the s.replace(12,... would not work. What happens if the replacement string is greater than 40 characters or the string to replace is greater than 13 characters? We need a more definitive description of the requirements.
Please try this ..
Open in new window
Hope it helps!