如何将相同的特殊字符替换为其他字符

时间:2021-02-04 19:35:53

I want to replace special character* to first Special character <b> and second same special character to </b> like that

我想将特殊字符*替换为第一个特殊字符,将第二个相同的特殊字符替换为

Example: This my original string

示例:这是我的原始字符串

Prime *Minister* Digital India programme is an attempt to create a *digitally empowered society*, with financial inclusion contributing to a robust *formal economy*. The main idea behind this vision is not just connectivity, but about how to leverage that connectivity in enabling consumers, small businesses, traders and farmers to harness *technology* to maximise efficiency and *productivity*

Prime *部长*数字印度计划旨在创建一个*数字授权社会*,金融包容有助于强大的*正规经济*。这一愿景背后的主要思想不仅仅是连通性,还包括如何利用这种连通性使消费者,小企业,贸易商和农民能够利用*技术*来最大限度地提高效率和*生产力*

I want to replace this special symbol like that

我想替换这样的特殊符号

Prime <b>Minister</b> Digital India programme is an attempt to create a <b>digitally empowered society</b>, with financial inclusion contributing to a robust <b>formal economy</b>. The main idea behind this vision is not just connectivity, but about how to leverage that connectivity in enabling consumers, small businesses, traders and farmers to harness <b>technology</b> to maximise efficiency and <b>productivity</b>

Prime 部长 数字印度计划旨在创建一个数字授权社会 ,金融包容性有助于强大的正规经济 。这一愿景背后的主要思想不仅仅是连通性,而是关于如何利用这种连通性使消费者,小企业,贸易商和农民能够利用技术 来最大限度地提高效率和生产力

Thanks in advance.

提前致谢。

1 个解决方案

#1


1  

I think a regex is probably the wrong tool for this. You're probably better off writing a simple loop to check your requirement.

我认为正则表达式可能是错误的工具。你可能最好写一个简单的循环来检查你的要求。

Something like this :

像这样的东西:

int counter =0;
for(int i=0;i!=-1;) {
    i=val.indexOf("*");
    if(counter%2==0) {
        val = val.replaceFirst("\\*","<b>");
    } else {
        val = val.replaceFirst("\\*","<\\\\b>");
    }
    counter++;
}

#1


1  

I think a regex is probably the wrong tool for this. You're probably better off writing a simple loop to check your requirement.

我认为正则表达式可能是错误的工具。你可能最好写一个简单的循环来检查你的要求。

Something like this :

像这样的东西:

int counter =0;
for(int i=0;i!=-1;) {
    i=val.indexOf("*");
    if(counter%2==0) {
        val = val.replaceFirst("\\*","<b>");
    } else {
        val = val.replaceFirst("\\*","<\\\\b>");
    }
    counter++;
}