需求:
1.可以支持中英文折行保存
2.输入字符串,可以对本字符串整理后返回整理后的字符串
3.可以指定每行的英文或数字字数。
我的实现函数如下:
我的代码实现:
//参数说明:传入的字符串chCleanStr
//每行的长度:iLineLen(英文)
//返回 0为成功
int CDoctor::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;
}