[英]Replace exact string with special characters and not the part of another substring
[英]Replace a part of substring with a string that is repeated
我想用*
s 替換包含"abc"
的子字符串的一部分。 我想使用 java8 來做到這一點( repeat()
在 java8 中不存在)。
String s = "abcdefgh";
String word1 = "abc";
s = s.replaceAll(word1,"*"*word1.length());
輸出應該是:
s = "***defgh"
你可以這樣做:
String s = "abcdefgh";
String word1 = "abc";
String replace = "";
for (int i = 0; i < word1.length(); i++) {
replace += "*";
}
s = s.replaceAll(word1,replace);
System.out.println(s);
給出:
***defgh
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.