【问题标题】:Case insensitive replace() method in Java (using indexOf)Java 中不区分大小写的 replace() 方法(使用 indexOf)
【发布时间】:2014-06-06 20:47:14
【问题描述】:

我需要一个 replace() 方法,它可以以不区分大小写的方式替换干草堆字符串中的针字符串。我还需要在没有任何正则表达式的情况下完成此操作。我找不到任何这样的方法,所以我自己写了。这个问题是要记录它,以防其他人将来发现它有用。如果可以进行任何改进(不使用 String.replace),请随时提出建议。

【问题讨论】:

  • 为什么不想使用replace()repalceAll() 方法?这背后有什么具体原因吗?
  • @Braj 我需要替换不区分大小写,而且,我正在使用 google web takeit,它将 java 代码编译为 javascript。所以正则表达式等必须与javascript兼容。因此,没有一种简单的方法可以转义用户输入,因此可以在 replaceAll 中使用它。
  • 你为什么担心Java代码到JavaScript?把它留给 GWT 编译器。如果 GWT 支持该 API 或方法,那么您可以在客户端使用它。
  • @Braj GWT 不支持 java 风格的正则表达式。

标签: java string replace substring indexof


【解决方案1】:
public static String replace(String needle, String hayStack, String replacement)
{
    String origNeedle = needle;
    String origHayStack = hayStack;

    needle = origNeedle.toLowerCase();
    hayStack = origHayStack.toLowerCase();

    int hayStackLen = hayStack.length();
    int needleLen = needle.length();
    int from = 0;
    int to;

    String stuffBeforeNeedle;
    StringBuilder output = new StringBuilder();

    do
    {
        to = hayStack.indexOf(needle, from);
        if (to == -1)
            to = hayStackLen;

        stuffBeforeNeedle = hayStack.substring(from, to);
        output.append(stuffBeforeNeedle);

        if (to < hayStackLen)
            output.append( replacement );

        from = hayStack.indexOf(needle, to) + needleLen;
    }
    while (to < hayStackLen);

    return output.toString();
}

【讨论】:

    【解决方案2】:
    public static void main(String[] args) throws IOException, ApplicationException, InterruptedException
    {
        String output = "";
    
        String haystack = "This is the end. The beautiful EnD.  No safety or surprise, the eND. La la la!";
        String needle = "eNd";
    
        String replacement = "beginning";
    
        String searchHaystack = haystack.toLowerCase();
        String searchNeedle = needle.toLowerCase();
    
        int substringStart = 0;
        int beginningOfNeedle = -1;
        while(true)
        {           
            // Finds the first needle in the haystack, starting the search just after the last one we found.
            // (On the first iteration, we start from the first character).
            beginningOfNeedle = searchHaystack.indexOf(searchNeedle, ++beginningOfNeedle);
    
            // If we can't find another needle, we're done.
            if(beginningOfNeedle == -1)
                break;          
    
            // If we found a needle, we add to our output the substring of haystack
            // that starts from substringStart and goes right up to the beginning of the needle
            // we just found.
            output += haystack.substring(substringStart, beginningOfNeedle);
            // We also add the replacement text.
            output += replacement;
    
            // The next substring will start right at the end of the needle.
            substringStart = beginningOfNeedle + needle.length();
        }
        // We add the last substring (which runs through the end of the haystack)
        // to the output.
        output += haystack.substring(substringStart);
    
    
        System.out.println(output);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 2013-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多