替换/截尾

时间:2011-02-18 13:32:43

标签: java replace

我正在使用一个叫做测验的系统......

剩下的最后一件事就是'线索'。目前我有

   <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();
    }
}

编译结果=&gt; 'Einst * in'

循环不起作用.. idk ..只替换数组中的第一个字母...

如果有人愿意帮助我,我将非常感激......

- 谢谢!

3 个答案:

答案 0 :(得分:3)

这样的事情怎么样?

public String generatClue(String answer,int level){
    if(level >= answer.length()/2)
        return answer.replaceAll("[^ ]","*");

    return answer.substring(0,level)
      + answer.substring(level,answer.length()-level).replaceAll("[^ ]","*")
      + answer.substring(answer.length()-level);
}

输出:

generateClue("Einstein",1);
=> E******n
generateClue("Einstein",3);
=> Ein**ein
generateClue("Einstein",4)
=> Einstein
generateClue("Hans Christian Andersen",4)
=> Hans ********* ****rsen

编辑:这是字符串中随机字符的一个:

public String generatClue2(String answer,int level){
    if(answer.length()==level)
        return answer.replaceAll("[^ ]","*");

    Random rand=new Random();

    for(int i=0; i<level; ++i){
        char c;
        int n;
        do{
            n=rand.nextInt(answer.length());
            c=answer.charAt(n);
        }
        while(c == ' ' || c == '*');
        answer = answer.substring(0,n) + '*' + answer.substring(n+1);
    }
    return answer;
}

输出:

generateClue2("Hans Christian Andersen",4);
=> Han* C*ri*tian Ande*sen
generateClue2("Hans Christian Andersen",4);
=> *ans Chr*sti*n An*ersen
generateClue2("Hans Christian Andersen",17);
=> H**s ******i** ***e****
generateClue2("Hans Christian Andersen",23);
=> **** ********* ********

答案 1 :(得分:1)

为什么不使用类似JDOM的东西并按名称获取XML元素?

SAXBuilder b = New SAXBuilder();
Document doc = b.build(pathToFile);
List<?> elements = b.getChildren("clue");
for (Element e : elements ) {
  (Element) e.setAttribute("value", 
    obfuscateClueText(e.getAttribute("value")); //updated, see below
}

在回复你的评论时,你能写一个生成随机线索符号的方法吗?

private String obfuscateClueText(String clueValue) {
  //do something with clueValue
  return obfuscatedValue;
}

答案 2 :(得分:0)

我在此方法之前尝试过

public class Test 
{
    static String s = "Hans Christian Andersen";

    public static void main(String[] args) 
    {
        char c = '*';
        System.out.println(replaceCharAt(s, 0, c));
    }

    public static String replaceCharAt(String s, int pos1, char c) 
    {
         StringBuffer buf = new StringBuffer(s);
         int max = (s.length()-3), contor = 0;

         while (contor < max)
         {
             for(int i = pos1; i < (s.length()); i++)
             {
                 if(Rnd.get(100) > 50 && contor < max)  
                 {
                     buf.setCharAt(i, c);
                     contor++;
                 }
             }
         }

         return buf.toString();
    }
}

问题是来自字符串的'SPACE'..

output: ***s*******i*n *nde**e*