Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if SMS message is in standard GSM alphabet

Tags:

c#

unicode

sms

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?

like image 689
Chatur Avatar asked Nov 02 '25 14:11

Chatur


1 Answers

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

like image 134
Simon Farrow Avatar answered Nov 04 '25 06:11

Simon Farrow