计算SMS长度/零件C# [英] Calculate SMS Length/Parts C#

查看:94
本文介绍了计算SMS长度/零件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求在他的应用程序中帮助一个朋友,该朋友有一个指示器/计数器,该指示器/计数器应向最终用户显示在文本框中已经写入了多少个字符,并且还在此书面文本中有多少部分/短信 ??.

I have been asked to help a friend in his application which has an indicator/counter that should show the end-user how many characters have been written in the text box and also how many parts in this written text/SMS?.

最简单的部分是有关使用TextBox1.Text.Length获取当前字符数/长度的信息,而另一部分可用于获取此SMS文本中的多少部分,具体取决于阿拉伯语/Unicode 英语/7Bit 语言,并且每种语言在GSM方面都有不同的规范,因为单个阿拉伯语消息的最大长度为70个字符,串联部分为67个字符,对于英语,单个消息为160个字符单部分,而153为串联部分.

The easiest part was about getting the current characters count/length by using TextBox1.Text.Length, but the other part which was resposible for getting how many parts in this SMS text depending on both Arabic/Unicode and English/7Bit languages, and each language has a different specifications at GSM's side, as the one single Arabic message is 70 characters maximum and 67 for concatenated parts, and for English, it is 160 for the one single part and 153 for the concatenated parts.

推荐答案

我们有两个选择,第一个是从移动运营商处获得带有编码参数的SMS,这有助于我们确定消息的语言.如果是 7Bit Unicode 消息,那么很容易检查给定的编码参数值并继续进行160或70检查,另一个选择是让我们自己的语言检查器.无论如何,我们使用了下面的代码,它可以完美地工作:

We had two options, the first one was that we were getting the SMS from the mobile operator with an encoding parameter which helped us to determine the language of the message if it was 7Bit or Unicode message, so it was easy to check the given encoding parameter value and go ahead with 160 or 70 check, and the other option was to have our own language checker. Anyways, we used the below code and it works perfectly:

public int CalculateSmsLength(string text)
{
    if (IsEnglishText(text))
    {
        return text.Length <= 160 ? 1 : Convert.ToInt32(Math.Ceiling(Convert.ToDouble(text.Length) / 153));
    }

    return text.Length <= 70 ? 1 : Convert.ToInt32(Math.Ceiling(Convert.ToDouble(text.Length) / 67));
}

public bool IsEnglishText(string text)
{
    return Regex.IsMatch(text, @"^[\u0000-\u007F]+$");
}

Math.Ceiling 返回大于或等于指定数字的最小整数.

Math.Ceiling returns the smallest integer greater than or equal to the specified number.

P.S..我们有一个应用程序正在检测给定的文本是否采用7Bit或Unicode编码,但这是一个很长的代码,以后会以适当的标题发布.

P.S. We had an application was detecting the given text if it was in 7Bit or Unicode encoding, but it is a long code and will post it later under an appropriate title.

这篇关于计算SMS长度/零件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆