Java String Replace replaceIgnoreCase(String line, String oldString, String newString)

Here you can find the source of replaceIgnoreCase(String line, String oldString, String newString)

Description

replace Ignore Case

License

Apache License

Declaration

public static final String replaceIgnoreCase(String line, String oldString, String newString) 

Method Source Code

//package com.java2s;
/**/* w w w . ja v  a2 s .c  o m*/
 * 
 * Copyright 2008 - 2009
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 * 
 * @project loonframework
 * @author chenpeng
 * @email?ceponline@yahoo.com.cn
 * @version 0.1
 */

import java.util.ArrayList;

import java.util.List;

public class Main {

    public static final String replaceIgnoreCase(String line, String oldString, String newString) {
        if (line == null)
            return null;
        String lcLine = line.toLowerCase();
        String lcOldString = oldString.toLowerCase();
        int i = 0;
        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = lcLine.indexOf(lcOldString, i)) > 0; j = i) {
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            return buf.toString();
        } else {
            return line;
        }
    }

    public static final String replaceIgnoreCase(String line, String oldString, String newString, int count[]) {
        if (line == null)
            return null;
        String lcLine = line.toLowerCase();
        String lcOldString = oldString.toLowerCase();
        int i = 0;
        if ((i = lcLine.indexOf(lcOldString, i)) >= 0) {
            int counter = 1;
            char line2[] = line.toCharArray();
            char newString2[] = newString.toCharArray();
            int oLength = oldString.length();
            StringBuffer buf = new StringBuffer(line2.length);
            buf.append(line2, 0, i).append(newString2);
            i += oLength;
            int j;
            for (j = i; (i = lcLine.indexOf(lcOldString, i)) > 0; j = i) {
                counter++;
                buf.append(line2, j, i - j).append(newString2);
                i += oLength;
            }

            buf.append(line2, j, line2.length - j);
            count[0] = counter;
            return buf.toString();
        } else {
            return line;
        }
    }

    public static int length(String s) {
        if (s == null)
            return 0;
        else
            return s.getBytes().length;
    }

    public static String toString(String str) {
        if (str == null || str.length() == 0)
            return "";
        else
            return str.trim();
    }

    public static String[] trim(String[] s) {
        if (s == null) {
            return null;
        }
        for (int i = 0, len = s.length; i < len; i++) {
            s[i] = s[i].trim();
        }
        return s;
    }

    public static String trim(String s, char delimit[]) {
        if (s == null) {
            return null;
        }
        int length = s.length();
        int beginIndex = 0;
        int endIndex = length;
        for (; beginIndex < length; beginIndex++) {
            char c = s.charAt(beginIndex);
            boolean found = false;
            for (int i = 0; i < delimit.length; i++) {
                if (delimit[i] != c) {
                    continue;
                }
                found = true;
                break;
            }
            if (!found)
                break;
        }

        for (; endIndex > beginIndex; endIndex--) {
            char c = s.charAt(endIndex - 1);
            boolean found = false;
            for (int i = 0; i < delimit.length; i++) {
                if (delimit[i] != c)
                    continue;
                found = true;
                break;
            }

            if (!found)
                break;
        }

        if (beginIndex == endIndex)
            return "";
        if (beginIndex > 0 || endIndex < length)
            return s.substring(beginIndex, endIndex);
        else
            return s;
    }

    public static List subString(String in, String open, String end) {
        List list = new ArrayList();
        while (true) {
            int from = in.indexOf(open, 0);
            if (from == -1)
                break;
            in = in.substring(from + open.length(), in.length());
            int to = in.indexOf(open, 0);
            int mid = in.indexOf(open);
            if ((mid > to || mid == -1) && to != -1) {
                list.add(in.substring(0, to));
            }
        }
        return list;

    }
}

Related

  1. replace(String string, String pattern, String value)
  2. replace(String target, String replacement, String... args)
  3. replaceAll(String source, String toReplace, String replacement)
  4. replaceEmitKeys(String map, Collection newKeys)
  5. replaceFirst(String input, String search, String replacement)
  6. replaceIgnoreCase(String source, String strBeReplace, String strReplaced)
  7. replaceNonPrintableAsciiCharacters(String str)
  8. replaceOnce(String template, String placeholder, String replacement)
  9. replaceSign(String sourceStr, char startc, char endc, String replaceStr)