繁体   English   中英

字符串替换为另一个字符C#

[英]String replace with another character C#

我的消息通常在SMSgateway中出现(网关将消息转发给我们的应用程序),

设置pØll12 3 4 5 6 7 8 121011(此消息是正确的)

但有些情况下这个消息就像,

sett p&oslas; ll 1 2 3 4 5 6 7 8 121011(此消息不正确,Ø附带&oslas;)

消息来自&oslas时,此消息以字符串形式出现; 我需要将它更换为Ø。 有时它带有另一个名字,比如gordØn(gord&oslas; n)它总是带有&符号&endup的属性; 标志。

在这里,我试图这样做,但我的知识还不足以完成这个..

 protected void Page_Load(object sender, EventArgs e)
{

    try
    {
        string mobileNo = Request.QueryString["msisdn"];
        int  dest = int.Parse(Request.QueryString["shortcode"].ToString());
        string messageIn = Request.QueryString["msg"];
        string operatorNew = Request.QueryString["operator"];

        string []reparr = messageIn.Split(' ');//split it & then check contains
        reparr[1].Contains = '&';
        reparr[1].Contains = ';';
        // In here i need to check that character & replace it to originalone.


        Label1.Text = "ok";

        WriteToFile(mobileNo, dest, messageIn, operatorNew,true,"","");

那些看起来像HTML实体。 您可以尝试使用HttpUtility.HtmlDecode解码它们:

string messageIn = HttpUtility.HtmlDecode(Request.QueryString["msg"]);

您可以使用HttpServerUtility.UrlDecode解码网址,请参阅http://msdn.microsoft.com/en-us/library/system.web.httpserverutility.urldecode.aspx

我不知道Web命名空间实用程序,但您可以使用下面的代码(如果&和;的位置始终位于消息的开头,并且这些字符不会出现在消息文本中):

String s = @"sett p&oslas;ll 1 2 3 4 5 6 7 8 121011";
Int32 startIndex = s.IndexOf("&");
String s1 = s.Replace(s.Substring(startIndex, s.IndexOf(";") - startIndex + 1), "Ø");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM