Checking if SMS message is in standard GSM alphabe

2019-07-10 05:03发布

问题:

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



标签: c# unicode sms