Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have problem in String.Replace() method

"a.set(a_Value, comment= a_Comment', report= 0, format= '%s')"

If I use Replace method in above string to replace "a", it replace "a" every where in String but I need to replace only "a" not "a_Value" and "a_Comment" ..
Posted
Updated 23-Jun-13 23:32pm
v2

Why not just replace "a." with "x." or whatever you need to replace it with?
 
Share this answer
 
Comments
Shahin Khorshidnia 24-Jun-13 5:55am    
Yup!
rahuls1 25-Jun-13 1:35am    
whatever i need that iwant to replace...
Then Replace is not for you - either use IndexOf and Substring to replace the data manually, or use a Regex to do a more sophisticated pattern matching and replacement.

From your brief description it is difficult to tell, but if you want to change:
a.set(a_Value, comment= a_Comment', report= 0, format= '%s')
To
Replaced.set(a_Value, comment= a_Comment', report= 0, format= '%s')
Then this might do it:
C#
Regex reg = new Regex("\\w+(?=\\.set)");
string result = reg.Replace(InputText,"Replaced");

But it's difficult to tell from a single example of the input with no output samples.
 
Share this answer
 
Hi
C#
var myString = "a.set a.set(a_Value, comment= a_Comment', report= 0, format= '%s')";

//If you want to replace only 'a.' to something else like 'b.':
var replaced = myString.Replace("a.", "b.");


//If you want to replace all 'a's except 'a_':

var sb = new StringBuilder();

for(var i =0;i<mystring.length;i++)>
{
    var c = myString[i];

    if (c=='a' && i < myString.Length - 1 && myString[i + 1]!= '_')
    {
        c = 'b';
    }
    sb.Append(c);
}

replaced = sb.ToString();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900