4

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?

pilsetnieks
  • 10,330
  • 12
  • 48
  • 60
Chatur
  • 331
  • 1
  • 8
  • 17
  • 3
    http://en.wikipedia.org/wiki/GSM_03.38 – Jon Apr 10 '13 at 08:45
  • Thanks for your reply but I know the alphabet, I am looking for a way that How can I check if a message contains any character out of this set. – Chatur Apr 10 '13 at 08:48
  • 2
    You just have to check each character in the message and see if any of them are not in the GSM alphabet. I don't understand what the problem is you are having with this? – Vicky Apr 10 '13 at 08:55
  • Putting the characters in an array or regular expression and then checking the characters of the message against this reference would be a good start. – Jon Apr 10 '13 at 08:56
  • 1
    Also bear in mind the GSM alphabet has some extended characters - single ASCII characters which are encoded as TWO GSM characters. If you have any of these in your message then your calculation will not work. – Vicky Apr 10 '13 at 08:57

1 Answers1

3

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

Simon Farrow
  • 1,901
  • 2
  • 20
  • 26
  • 2
    This regex is broken, see http://stackoverflow.com/questions/29541753/regex-only-checks-first-character-in-string-c-sharp/29541980#29541977. – CodeCaster Apr 09 '15 at 15:06