替换功能未替换旧值 [英] replace function didn't replace the old value

查看:72
本文介绍了替换功能未替换旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


这是我的代码,我试图将特定的字符串替换为空,但没有发生,我不确定这里缺少什么.

Hi
here is my code,am trying to replace a particular sting to empty but its not happening,am not sure what am missing here.

try
{
   transformedMsg = messageTransformer.transform@"C:\test.xsl",msgBody);
   //transformedMsg  returns value <?xml version="1.0" encoding="utf-16"?>
   string strxml = "<?xml version=" + "1.0" + " encoding=" + "utf-8" + "?>";
   string validtransmsg = transformedMsg.Replace(strxml, "");
   if(validtransmsg.Trim().Length>1)
   {
        DbMessageLoader.loadTransformedMessage(originalMsgId, msgTypeId, transformedMsg, sender);
   }
}
catch (Exception ex)
{
   logMessage("Transformation failed in External service: " + ex.Message + originalMsgId );
}





transformMsg返回值,我试图将此值替换为空白,然后我会找到长度.
但它不能将值替换为空,请告诉我这里缺少什么





transformedMsg returns value ,am trying to replace this value to empty space then i will find the length.
but its not replace the value to empty,please advise me what am missing here

推荐答案

记住,System.String的实例是不可变的对象.请查看我的解释以及我过去的回答:
C#中的可变字符串和不可变字符串之间的区别 [ ^ ].

-SA
Remember, the instance of System.String is an immutable object. Please see my explanation and some implications in my past answer:
Difference Between Mutable , Immutable string in C#[^].

—SA


String.Replace不会替换原始变量的值.而是将输出保存到新变量.

如果您别无选择,仍然需要更改它,请继续进行以下操作.
String.Replace doesnt replace the original variable''s value. Instead the output is saved to the new variable.

if u got no choice and still need to change it, then go for the below.
transformedMsg = transformedMsg.Replace(strxml, "");



http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx[^]

public class ReplaceTest {
    public static void Main() {

        string errString = "This docment uses 3 other docments to docment the docmentation";

        Console.WriteLine("The original string is:{0}''{1}''{0}", Environment.NewLine, errString);

        // Correct the spelling of "document".

        string correctString = errString.Replace("docment", "document");

        Console.WriteLine("After correcting the string, the result is:{0}''{1}''",
                Environment.NewLine, correctString);
    }
}
//
// This code example produces the following output:
//
// The original string is:
// ''This docment uses 3 other docments to docment the docmentation''
//
// After correcting the string, the result is:
// ''This document uses 3 other documents to document the documentation''
//




干杯
Balaji




Cheers
Balaji


J a a n s-4天前
使用字符串strxml =<?xml version = \" 1.0 \"encoding = \" utf-8 \?>";还要检查编码(utf-8或utf-16).


感谢Jaans.
J a a n s - 4 days ago
Use string strxml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>"; Also check the encoding (utf-8 or utf-16).


Thanks to Jaans.


这篇关于替换功能未替换旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆