简体   繁体   English

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

[英]String replace with another character C#

my messages normally comes like this in SMSgateway(gateway forwarded the message to our application), 我的消息通常在SMSgateway中出现(网关将消息转发给我们的应用程序),

sett pØll 1 2 3 4 5 6 7 8 121011 ( this message is correct ) 设置pØll12 3 4 5 6 7 8 121011(此消息是正确的)

But some cases this message comes like, 但有些情况下这个消息就像,

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

this message comes as a string,when message comes &oslas; 消息来自&oslas时,此消息以字符串形式出现; i need to replace it to Ø. 我需要将它更换为Ø。 sometimes it comes with another name, like gordØn(gord&oslas;n) It always stat with & sign & endsup with ; 有时它带有另一个名字,比如gordØn(gord&oslas; n)它总是带有&符号&endup的属性; sign. 标志。

Here i tries to do it,but my knowledge is not enough to complete this.. 在这里,我试图这样做,但我的知识还不足以完成这个..

 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,"","");

Those look like HTML entities. 那些看起来像HTML实体。 You can try to decode them using HttpUtility.HtmlDecode : 您可以尝试使用HttpUtility.HtmlDecode解码它们:

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

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

I am not aware about web namespace utilities but you may use below code (if position of & and ; is always going to be in beginning of message and these characters will not appear inside message text): 我不知道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