char*, wchar_t* 替换子串
替换 子串 char
2023-09-11 14:13:59 时间
char* replace_a(char*s1,char*s2,char*s3=NULL)
{
char *p,*from,*to,*begin=s1;
int c1,c2,c3,c; //串长度及计数
c2=strlen(s2);
c3=(s3!=NULL)?strlen(s3):0;
if(c2==0)return s1; //注意要退出
while(true) //替换所有出现的串
{
c1=strlen(begin);
p=strstr(begin,s2); //出现位置
if(p==NULL) //没找到
return s1;
if(c2>c3) //串往前移
{
from=p+c2;
to=p+c3;
c=c1-c2+begin-p+1;
while(c--)
*to++=*from++;
}
else if(c2<c3) //串往后移
{
from=begin+c1;
to=from-c2+c3;
c=from-p-c2+1;
while(c--)
*to--=*from--;
}
if(c3) //完成替换
{
from=s3,to=p,c=c3;
while(c--)
*to++=*from++;
}
begin=p+c3; //新的查找位置
}
}
wchar_t* replace_w(wchar_t* s1, wchar_t* s2, wchar_t* s3 = NULL)
{
wchar_t* p, * from, * to, * begin = s1;
int c1, c2, c3, c; //串长度及计数
c2 = wcslen(s2);
c3 = (s3 != NULL) ? wcslen(s3) : 0;
if (c2 == 0)return s1; //注意要退出
while (true) //替换所有出现的串
{
c1 = wcslen(begin);
p = wcsstr(begin, s2); //出现位置
if (p == NULL) //没找到
return s1;
if (c2 > c3) //串往前移
{
from = p + c2;
to = p + c3;
c = c1 - c2 + begin - p + 1;
while (c--)
* to++ = *from++;
}
else if (c2 < c3) //串往后移
{
from = begin + c1;
to = from - c2 + c3;
c = from - p - c2 + 1;
while (c--)
* to-- = *from--;
}
if (c3) //完成替换
{
from = s3, to = p, c = c3;
while (c--)
* to++ = *from++;
}
begin = p + c3; //新的查找位置
}
}
相关文章
- Word控件Spire.Doc 【文本】教程(21) ;如何在 C# 中用 Word 文档替换文本
- 【Vue/js】Js中执行变量中的命令语句,也就是所谓的宏替换(很实用的例子)
- 微信小程序轮子 - 瀑布流布局+数据懒加载(可替换图片瀑布流懒加载 / 视频瀑布流懒加载等等)
- 如何替换vi的配色方案
- .bat批处理(六):替换字符串中匹配的子串
- 实用技巧 ——Eclipse全局搜索和替换
- 直接替换Springboot jar包中的文件
- Shell脚本学习指南(二)——查找与替换
- 安卓中MessageQueue能否被替换为BlockQueue?
- MySQL REPLACE替换输出
- 来n遍剑指--05. 替换空格
- full-SDK替换MacOS安全告警处理
- 替换加密、解密算法
- OC中字符串的提取与替换-四种不同方法实现