我正在致力於一個名爲Quiz的系統...替換/被刪掉已刪除/刪除內容
剩下的最後一件事就是'線索'。在目前我還
<id value="100">
<question value="Who said E=mc2"/>
<answear value="Einstein"/>
<clue1 value="E*******"/>
<clue2 value="E******n"/>
<clue3 value="Ei****in"/>
</id>
,我想從XML刪除的線索,因爲很難做手工他們... 所以我做了什麼,但我失敗了
public class Test
{
public static void main(String[] argv) throws Exception
{
System.out.println(replaceSubString("Einstein", "*", 3));
}
static String[] letters = {"e","i"};
public static String replaceSubString(final String str, final String newToken, int max)
{
if ((str == null) || (newToken == null))
return str;
StringBuffer buf = new StringBuffer(str.length());
int start = 0, end = 0;
for(int i = 0; i < letters.length; i++)
{
if(Rnd.get(100) > 50) //50% to add the symbol
{
while ((end = str.indexOf(letters[i], start)) != -1)
{
buf.append(str.substring(start, end)).append(newToken);
start = end + 1;
if (--max == 0)
break;
}
}
}
buf.append(str.substring(start));
return buf.toString();
}
}
編譯結果=>' Einst *在
循環就不會工作。IDK ..只被替換從數組的第一個字母...
,如果有人提供幫助我,我將不勝感激..
- 謝謝!
要生成的線索,這就是它? – 2011-02-18 13:42:03
循環確實有效,也許你應該測試更多的文字和字母? – Ishtar 2011-02-18 13:47:57
@Nicolas Repiquet - 是的..就是這樣 – 2011-02-18 14:15:45