Checking if SMS message is in standard GSM alphabet

自作多情 提交于 2020-01-04 04:37:23

问题


I am using an API for sending SMS and I need to calculate number of SMSs in a message.

If the message uses only the GSM alphabet characters, it can be up to 160 characters in length but if a message contains any characters outside this alphabet, it will be encoded as Unicode (UCS-2) and then it can have only up to 70 characters in one SMS. When sending concatenated, i.e., multi-part messages, each part can be only up to 153 or 67 characters in length, respectively.

I am using C# to send messages, how can I check if the message will contain only GSM alphabet characters?


回答1:


You can do it with a pretty horrible regular expression. Here is an extension method.

public static bool IsUnicodeSms(this string message)
{
    var strMap = new Regex(@"^[@£$¥èéùìòÇØøÅå_ÆæßÉ!""#%&'()*+,-./0123456789:;<=>? ¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^{}\[~]|€]+$");
    return !strMap.IsMatch(message);
}

Enjoy



来源:https://stackoverflow.com/questions/15921013/checking-if-sms-message-is-in-standard-gsm-alphabet

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!