簡體   English   中英

用重復的字符串替換子字符串的一部分

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM