检查字符串是否具有特定模式

huangapple go评论68阅读模式
英文:

checking a string for specific pattern

问题

Sure, here is the translated content:

我有一个大字符串。我想要查找每个<img src="rabbit.jpeg">,并且例如,将其替换为<img src="arabit.jpeg">
文件名每次可能都不同。我想要在文件名之前添加一些内容。谢谢。

英文:

I have a large string. I want to look for every <img src="rabbit.jpeg"> and for example, replace it with <img src="arabit.jpeg">.
File name can be different everytime. I want to add something before the filename. Thanks

答案1

得分: 0

我认为您正在寻找replace方法。

public class Guru99Ex1 {
    public static void main(String args[]) {
        String S1 = new String("the quick fox jumped");
        System.out.println("原始字符串为: " + S1);
        System.out.println("替换'fox'为'dog'后的字符串: " + S1.replace("fox", "dog"));
        System.out.println("替换所有't'为'a'后的字符串: " + S1.replace('t', 'a'));
    }
}

Java中有这个字符串的`replace`方法我猜这个方法适合您的需求
英文:

İ think you are looking for replace method.

public class Guru99Ex1 {
public static void main(String args[]) {
    String S1 = new String("the quick fox jumped");
    System.out.println("Original String is ': " + S1);
    System.out.println("String after replacing 'fox' with 'dog': " + S1.replace("fox", "dog"));
    System.out.println("String after replacing all 't' with 'a': " + S1.replace('t', 'a'));

}

}

Java has this replace method for String. i guess it will do the job for you.

答案2

得分: 0

你需要使用replaceAll函数。

尝试像这样:

String line = "一些文本 <img src=\"rabbit.jpeg\"> 一些文本 <img src=\"rabbit.jpeg\"> 一些文本";
line = line.replaceAll("<img src=\"rabbit.jpeg\">", "<img src=\"arabit.jpeg\">");
System.out.println(line);
英文:

You need to use replaceAll function..

Try like this:

String line = &quot;some text &lt;img src=\&quot;rabbit.jpeg\&quot;&gt;  some text &lt;img src=\&quot;rabbit.jpeg\&quot;&gt; some text&quot;;
line = line.replaceAll(&quot;&lt;img src=\&quot;rabbit.jpeg\&quot;&gt;&quot;,&quot;&lt;img src=\&quot;arabit.jpeg\&quot;&gt;&quot;);
System.out.println(line);

huangapple
  • 本文由 发表于 2020年5月3日 14:53:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61570697.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定