简体   繁体   中英

Case insensitive replace() method in Java (using indexOf)

I've needed a replace() method which could replace a needle String within a haystack String, in a case insensitive way. I also needed this to be done without any regex. I couldn't find any such method, so I've written my own. This question is to document it, in case anyone else finds it useful in future. And if any improvements can be made (without using String.replace), feel free to suggest them.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM