I'm new in programming. I just need someone who开发者_Python百科 can tell me how to replace string values in C#? The values which I'm referring to are dynamic which means I cannot use .Replace .
Yes you can use Replace
String toBeReplaced = "can't";
String toBeReplacedWith = "can";
String sentence="I can't use Replace";
sentence = sentence.Replace(toBeReplaced,toBeReplacedWith);
sentence becomes "I can use Replace"
I'm not sure but it depends on what object you are going to hold in this dynamic
field, then you can make a decision to chose the correct way :
dynamic tempDynamic = "hello";
Type objectType = tempDynamic.GetType();
if (objectType == typeof(String))
{
string tempStr = tempDynamic.ToString();
tempStr = tempStr.Replace("hello", "goodbye");
tempDynamic = tempStr;
// at this time do what ever you like with your dynamic
}
else
{
// Go with another ...
}
In this way You have to ensure of what types your dynamic
is going to hold.
Hope this help.
If you already know what types your dynamic
is going to hold, try this:
//lets assume that `dynamicStuff.whatever` is a string//
dynamic dynamicStuff;
string dynStr = dynamicStuff.whatever;
dynStr = dynStr.Replace("Your replace string");
精彩评论