I’m working to a system called Quiz …
The last thing that remains are the ‘clues’. In present i have
<id value="100">
<question value="Who said E=mc2"/>
<answear value="Einstein"/>
<clue1 value="E*******"/>
<clue2 value="E******n"/>
<clue3 value="Ei****in"/>
</id>
and I want to remove from xml the clues because is hard to do them manually …
so I made something but i failed
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();
}
}
Compile Result => ‘Einst*in’
the loop doesn’t work.. idk .. only the first letter from array is replaced…
if someone offers to help me I would be very grateful..
-Thanks !
How about something like this?
Output:
EDIT: Here’s one for random characters in the string:
Output: