Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

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 "XXXhblahblahblahblahblahblahblahblahXXX"
so i did like this

string replace = "XXXhblahblahblahblahblahblahblahblahXXX"
s = s.replace (12,12+40, replace);

it works but my final sting is like below
"hi this is XXXhblahblahblahblahblahblahblahblahXXX"

instead I expected
"hi this is XXXhblahblahblahblahblahblahblahblahXXXabcdefghijklmnopqurstu"

so how can i insert a string also replace an existing string.

Thanks,
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Hi Shragi,
Please try this ..

--

s = s.replace("testuser     ", "XXXhblahblahblahblahblahblahblahblahXXX");

--

Open in new window


Hope it helps!
Avatar of shragi

ASKER

Hi Pawan - you are hard coding it, I am trying to replace string fron 12th index.

Thanks,
Hi,
Please try like this ..

s = s.replace (12,12+8, replace);
Avatar of shragi

ASKER

Hi Pawan - Here in this example I gave the replacement string as "XXXhblahblahblahblahblahblahblahblahXXX" 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
"hi this is XXXhblahblahblahbl                         abcdefghijklmnopqurstu"

Open in new window


so irrespective of replacement string i want to replace a 13char with 40 char string
Avatar of shragi

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.
ASKER CERTIFIED SOLUTION
Avatar of Pawan Kumar
Pawan Kumar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of CEHJ
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
Hi Author,
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("XXXhblahblahblahbl");
//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.