短信分割

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

/// <summary>
/// checkSms 的摘要说明
/// </summary>
public class checkSms
{
 public checkSms()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
 }

    private const String PRE_STR = "(续)";  
    private const String SUF_STR = "(待续)";  
    private static int PRE_STR_LEN = PRE_STR.Length;  
    private static int SUF_STR_LEN = SUF_STR.Length;  
    private static int EXT_LENGTH = PRE_STR_LEN + SUF_STR_LEN;  
    private static int MAX_LEN = 70; //每个短信最长大小  
     
    /// <summary>  
    /// 切割字符串,使每串不超过70个字符  
    /// </summary>  
    /// <param name="str"></param>  
    /// <returns></returns>  
    public List<String> StringSplit(String str)  
    {  
        List<String> msms = new List<string>();  
        bool star = true;  
        while (true)  
        {  
            if (!star)  
            {  
                str = PRE_STR + str;  
            }  
            int lenght = str.Length;  
            int msgCoun = getMsgCount(lenght);  
            int nullSpace = getLastMsgLeftCount(lenght, msgCoun);  
            if (str.Length <= MAX_LEN)  
            {  
                //不用分割  
                msms.Add(str);  
                break;  
            }  
            else 
            {  
                int splitIndex = getSplitIndex(str, nullSpace);  
                string tempStr = str.Substring(0, splitIndex);  
                msms.Add(tempStr + SUF_STR);  
                str = str.Substring(splitIndex);  
            }  
            star = false;  
        }  
        return msms;  
    }  
     
    /// <summary>  
    /// 获取一条短信最小可以分多少条短信  
    /// </summary>  
    /// <param name="t">短信长度</param>  
    /// <returns></returns>  
    private int getMsgCount(int t)  
    {  
        if (t < MAX_LEN)  
            return 1;  
        int n = (int)((t + MAX_LEN - 2 * EXT_LENGTH - 1) / (MAX_LEN - EXT_LENGTH));  
        return n;  
    }  
     
    /// <summary>  
    /// 计算最后一条信息还可添加多少个字符  
    /// </summary>  
    /// <param name="t">短信总字符数</param>  
    /// <param name="n">短信理想情况分页数</param>  
    /// <returns></returns>  
    private int getLastMsgLeftCount(int t,int n)  
    {  
        if (t <= MAX_LEN)  
            return MAX_LEN - t;  
        int l = MAX_LEN + (MAX_LEN - EXT_LENGTH) * (n - 1) - t;  
        return l;  
    }  
     
    private static char[] splitChar = new char[] { ',',' ','.',';','!',')','>','}',']',',',' ','。',';','!',')','》','/n',' /t'};  
     
    /// <summary>  
    /// 比较智能地去判断分割的位置,尽量在有分隔符的地方分割  
    /// </summary>  
    /// <param name="str">str的长度一定大于MAX_LEN</param>  
    /// <param name="space">剩余可用空间大小</param>  
    /// <returns></returns>  
    private int getSplitIndex(String str,int space)  
    {  
        if (space > 0)  
        {  
            for (int i = MAX_LEN - SUF_STR_LEN - 1, d = (MAX_LEN - SUF_STR_LEN - space - 1); i >= d; --i)  
            {  
                foreach (char c in splitChar)  
                {  
                    if (str[i] == c)  
                    {  
     
                        //匹配  
                        Console.WriteLine(i + 1);  
                        return i + 1;  
                    }  
                }  
            }  
        }  
        return MAX_LEN - SUF_STR_LEN;  
    }  

}

 

 

string str = "如果短信的长度超过70个字符,程序就会自动根据短信中的分隔符进行分割,在不是非常苛刻的情况下,它不会将一个词分成两段来发,比如,不会出现/"你 好/"的前一个字在第一条短信内,而/"好/"字在第二条短信内!这都是在不增加短信数量的情况下优化的!";  
        //List<String> ms = new MobileRegistService().StringSplit(str);  
        List<String> ms = new checkSms().StringSplit(str);
        foreach (String m in ms)  
        {  
            Response.Write(m + "==>" + m.Length);  
        }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值