英文:
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 = "some text <img src=\"rabbit.jpeg\"> some text <img src=\"rabbit.jpeg\"> some text";
line = line.replaceAll("<img src=\"rabbit.jpeg\">","<img src=\"arabit.jpeg\">");
System.out.println(line);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论