2014-12-03 109 views

回答

2

試試這個方法...

public static String StrRpl(String str) { 

    char[] chars = str.toCharArray(); 
    for (int i = 0, j = 0; i < chars.length && j < 5; i++) { 
     char ch = chars[i]; 
     if (!Character.isWhitespace(ch)) { 
      chars[i] = '*'; 
      j++; 
     } 
    } 
    str = new String(chars); 
    return str; 
} 

輸出: ***** 234

通這個方法的一個字符串,它會返回帶有'*'字符的字符串,直到前5個字符(你可以改變你的計數值,電流是5)