2016-02-01 203 views
0

我的字符串的確切數字是「測試」 「測試」有4個字符 我要替換「測試」與「****」 所以我得到「* ***java中字符串替換字符串與字符

我的代碼

System.out.println("_test_"); 
System.out.println("_test_".replaceAll("test", "*")); 

但與1代替測試*。

+1

''代替在'的replaceAll()' –

+1

當然test',則明確告訴它來代替'測試'用一個'*'。 – Andreas

+0

@noob幾乎。 ''_test _「。replaceAll(」[A-Za-z] +「,」*「)'將用一個'*'替換整個'test'。如果我們想要替換每個* single *字符,我們不需要那個'+'。 – Pshemo

回答

0

所以我得到了答案,我真的找 東西以儘可能少的線路越好。謝謝大家 的答案,但這是我發現最有用的答案。

對於在問題中不清楚,我很抱歉,如果我不是。

String str1 = "_AnyString_"; 
int start_underscore = str1.indexOf("_"); 
int end_underscore = str1.indexOf("_", start_underscore + 1); 
String str_anything = str1.substring(start_underscore + 1, end_underscore); 
String str_replace_asterisk = str_anything.replaceAll(".", "*"); 
System.out.println(str_replace_asterisk); 
str1 = str1.replace(str_anything, str_replace_asterisk); 
System.out.println(str1); 

輸出:使用`[A-ZA-Z] +的

_AnyString_ 
_*********_ 
0

是的,因爲replaceAll(str1, str2)將用str2代替所有出現的str1。由於您使用的文字,你需要說

System.out.println("_test_".replaceAll("test", "****")); 

如果你想讓自己的替換功能,你可以做這樣的事情:

public static String replaceStringWithChar(String src, String seek, char replacement) 
{ 
    StringBuilder sb = new StringBuilder(); 
    for(int i = 0; i < seek.length(); i++) sb.append(replacement); 
    return src.replaceAll(seek, sb.toString()); 
} 

你可以這樣調用它像這樣:

replaceStringWithChar("_test_", "test", '*'); 
+0

這是對這個問題的一個誤解,OP實際上是要求給定一串任意長度的字符串,他想用星星來替換整個單詞。 '''Test'''只是一個帶有4個字母的字符串的例子。 – DominicEU

+0

在循環中執行'String + = String'對性能不利。 – Andreas

3

如果單詞test只是一個示例,您可以使用Matcher.appendReplacement(有關此技術的更多詳細信息,請參閱How to appendReplacement on a Matcher group instead of the whole pattern?):

String fileText = "_test_"; 
String pattern = "test"; 
Pattern r = Pattern.compile(pattern); 
Matcher m = r.matcher(fileText); 
StringBuffer sb = new StringBuffer(); 
while (m.find()) { 
    m.appendReplacement(sb, repeat("*", m.group(0).length())); 
} 
m.appendTail(sb); // append the rest of the contents 
System.out.println(sb); 

而且repeat功能(從Simple way to repeat a String in java借來的,看到其他選擇),所以文章是:

public static String repeat(String s, int n) { 
    if(s == null) { 
     return null; 
    } 
    final StringBuilder sb = new StringBuilder(s.length() * n); 
    for(int i = 0; i < n; i++) { 
     sb.append(s); 
    } 
    return sb.toString(); 
} 

IDEONE demo

+0

在循環內部執行'repeat'對性能不利。 – Andreas

+0

還有[StringUtils.repeat](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringUtils.html#repeat)可以幫助: - ) – Enissay

+0

這就是爲什麼我添加了鏈接到我從中借用該方法的SO問題。我使用這個**僅用於演示**,因爲我無法在IDEONE上使用StringUtils。 –

0

如果你有一個任意文本將被替換,並且希望要使用replaceAll(),請注意,它需要一個正則表達式,並且各種字符具有特殊含義。爲防止出現問題,請致電Pattern.quote()

此外,要替換爲等長的*的序列,則需要構建這樣的字符串。

這裏是做一個漂亮的短方法:

private static String mask(String input, String codeword) { 
    char[] buf = new char[codeword.length()]; 
    Arrays.fill(buf, '*'); 
    return input.replaceAll(Pattern.quote(codeword), new String(buf)); 
} 

測試

System.out.println(mask("_test_", "test")); 
System.out.println(mask("This is his last chance", "is")); 

輸出

_****_ 
Th** ** h** last chance 
-1

其實你是非常接近你想要什麼。這是你可以做什麼:

System.out.println("_test_".replaceAll("[test]", "*")); 
System.out.println("hello".replaceAll("[el]", "*")); 

輸出:

_****_ 
h***o 
+0

不好,因爲'「hello」.replaceAll(「[test]」,「*」)'會生成'h * llo',即使字符串不包含'test',也會替換'e'。 – Andreas

+0

@Andreas OP沒有說他希望整個文本序列匹配被替換。 – user3437460