为什么我无法使用 string.replaceAll() 方法将“/abcd/”替换为“/xyz/”
Why I am not able to replace "/abcd/" to "/xyz/" using string.replaceAll() method
我有一个包含许多 url 字符串的大字符串,我想替换所有 url 的上下文。例如
https://host:port/sometext/abc
我想像这样用 /newtext/
替换 /sometext/
mystring.replaceAll("/sometext/", "/newtext/");
我不能只搜索 sometext
并替换,因为 sometext
可能已在许多地方使用。
但即使我也尝试了替换方法,它也不起作用,但它也不起作用。我做错了什么吗?
提前致谢
在Java中,字符串class是不可变的。您可以在 https://www.javatpoint.com/immutable-string 上进行探索。
您的问题的答案是以下代码:
String mystring = "https://host:port/sometext/abc";
mystring = mystring.replaceAll("/sometext/", "/newtext/");
System.out.println(mystring);
我有一个包含许多 url 字符串的大字符串,我想替换所有 url 的上下文。例如
https://host:port/sometext/abc
我想像这样用 /newtext/
替换 /sometext/
mystring.replaceAll("/sometext/", "/newtext/");
我不能只搜索 sometext
并替换,因为 sometext
可能已在许多地方使用。
但即使我也尝试了替换方法,它也不起作用,但它也不起作用。我做错了什么吗?
提前致谢
在Java中,字符串class是不可变的。您可以在 https://www.javatpoint.com/immutable-string 上进行探索。 您的问题的答案是以下代码:
String mystring = "https://host:port/sometext/abc";
mystring = mystring.replaceAll("/sometext/", "/newtext/");
System.out.println(mystring);