Link to home
Start Free TrialLog in
Avatar of chaitu chaitu
chaitu chaituFlag for India

asked on

how to replace one specific string in whole string

String namespace = "@{XYZ/1.0}categories:";
            String result= "(\"workspace://123456\" AND \"workspace://55757575\")";
            
            How to append namespace for every workspace in result string .so final output will be
            
            
            (@{XYZ/1.0}categories:"workspace://123456" AND @{XYZ/1.0}categories:"workspace://55757575");
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Well you could find out if and where to append with

int startNamespace = s.indexOf(namespace);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium 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
If you really want to use replaceAll (using regex), you can do the following (the search string contains metacharacters so needs quoting)

String nq = "\\Q" + namespace + "\\E";
s = s.replaceAll(nq, namespace + result);

Open in new window

You need to use return value of replaceAll() method. replaceAll() does not replace the characters in the current string, it returns a new string with replacement.

  • String objects are immutable, their values cannot be changed after they are created.
  • You may use replace() instead of replaceAll() if you don't need regex.
 
 
 String str = "abcd=0; efgh=1";
    String replacedStr = str.replaceAll("abcd", "dddd");

Open in new window


System.out.println(str);
    System.out.println(replacedStr);

Open in new window


outputs

abcd=0; efgh=1
dddd=0; efgh=1

Open in new window

Thanx 4 axxepting
How to append namespace for every workspace in result string .so final output will be
Hang on - i think i misunderstood. Did you mean how do you PREpend namespace?
Avatar of chaitu chaitu

ASKER

yes