简体   繁体   中英

String replace with another character C#

my messages normally comes like this in SMSgateway(gateway forwarded the message to our application),

sett pØll 1 2 3 4 5 6 7 8 121011 ( this message is correct )

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

this message comes as a string,when message comes &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 ; 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. You can try to decode them using 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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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