中英文折行保存函数

需求:

1.可以支持中英文折行保存

2.输入字符串,可以对本字符串整理后返回整理后的字符串

3.可以指定每行的英文或数字字数。

 

 

我的实现函数如下:

我的代码实现:

int CleanUpString(char* chCleanStr, int iLineLen)
{
    //传入字符长度
    int inputLen = strlen(chCleanStr);
    //没有内容不清理
    if (inputLen == 0)
    {
        return -1;
    }
    //iLienLen小于等于0时
    if (iLineLen <= 0)
    {
        return -2;
    }
    //更改后的长度
    int outputLen =inputLen + inputLen/iLineLen*2+1;
    //保存字符串暂用空间
    char* tempStr= new char[outputLen];
    //每行保存的字符以中文的双字节为主
    int ilenMax = 0;
    //读到的最后一个位置
    int tempStrPos=0;
    for (int i=0; i<strlen(chCleanStr);)
    {
        //汉字拷贝
        if (chCleanStr[i] > 127 || chCleanStr[i] < 0)
        {
            tempStr[tempStrPos] = chCleanStr[i];
            i=i+1;
            tempStrPos++;
            tempStr[tempStrPos] = chCleanStr[i];
            //下一个字处理
            i++;
            tempStrPos++;
            //每行现在字符个数
            ilenMax=ilenMax+2;
        }
        //原来字符串中有换行
        else if ('\r'==chCleanStr[i] || '\n'==chCleanStr[i])
        {
            tempStr[tempStrPos] = chCleanStr[i];
            ilenMax = 0;
            tempStrPos++;
            i++;
        }
        //数字英文拷贝
        else if (chCleanStr[i] <=127)
        {
            tempStr[tempStrPos] = chCleanStr[i];
            //下一个字处理
            i++;
            tempStrPos++;
            //每行现在字符个数
            ilenMax++;
        }
        //换行
        if (ilenMax>=iLineLen)
        {
            ilenMax = 0;
            tempStr[tempStrPos] = '\r';
            tempStrPos++;
            tempStr[tempStrPos] = '\n';
            tempStrPos++;
        }
    }
    tempStr[tempStrPos]='\0';
    //清空原有字符串
    memset(chCleanStr,0,inputLen);
    strcpy(chCleanStr,tempStr);
    delete []tempStr;
    return 0;
}

 

From:http://blog.csdn.net/dingdingko/article/details/2959510

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值