Java Array Replace replaceIgnoreCase(final String s, final String[] sub, final String[] with)

Here you can find the source of replaceIgnoreCase(final String s, final String[] sub, final String[] with)

Description

replace Ignore Case

License

Open Source License

Declaration

public static String replaceIgnoreCase(final String s, final String[] sub, final String[] with) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    public static String replaceIgnoreCase(final String s, final String[] sub, final String[] with) {
        if (sub.length != with.length || sub.length == 0) {
            return s;
        }//  w  w  w.  j  a v a 2  s . c  o m
        int start = 0;
        final StringBuilder buf = new StringBuilder(s.length());
        while (true) {
            final int[] res = indexOfIgnoreCase(s, sub, start);
            if (res == null) {
                break;
            }
            final int end = res[1];
            buf.append(s.substring(start, end));
            buf.append(with[res[0]]);
            start = end + sub[0].length();
        }
        buf.append(s.substring(start));
        return buf.toString();
    }

    public static int indexOfIgnoreCase(final String src, char c, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        final int srclen = src.length();
        if (endIndex > srclen) {
            endIndex = srclen;
        }
        c = Character.toLowerCase(c);
        for (int i = startIndex; i < endIndex; ++i) {
            if (Character.toLowerCase(src.charAt(i)) == c) {
                return i;
            }
        }
        return -1;
    }

    public static int indexOfIgnoreCase(final String src, final String subS) {
        return indexOfIgnoreCase(src, subS, 0, src.length());
    }

    public static int indexOfIgnoreCase(final String src, final String subS, final int startIndex) {
        return indexOfIgnoreCase(src, subS, startIndex, src.length());
    }

    public static int indexOfIgnoreCase(final String src, String sub, int startIndex, int endIndex) {
        if (startIndex < 0) {
            startIndex = 0;
        }
        final int srclen = src.length();
        if (endIndex > srclen) {
            endIndex = srclen;
        }
        final int sublen = sub.length();
        if (sublen == 0) {
            return (startIndex > srclen) ? srclen : startIndex;
        }
        sub = sub.toLowerCase();
        final int total = endIndex - sublen + 1;
        final char c = sub.charAt(0);
        Label_0145: for (int i = startIndex; i < total; ++i) {
            if (Character.toLowerCase(src.charAt(i)) == c) {
                for (int j = 1, k = i + 1; j < sublen; ++j, ++k) {
                    final char source = Character.toLowerCase(src.charAt(k));
                    if (sub.charAt(j) != source) {
                        continue Label_0145;
                    }
                }
                return i;
            }
        }
        return -1;
    }

    public static int[] indexOfIgnoreCase(final String s, final String[] arr) {
        return indexOfIgnoreCase(s, arr, 0);
    }

    public static int[] indexOfIgnoreCase(final String s, final String[] arr, final int start) {
        final int arrLen = arr.length;
        int index = Integer.MAX_VALUE;
        int last = -1;
        for (int j = 0; j < arrLen; ++j) {
            final int i = indexOfIgnoreCase(s, arr[j], start);
            if (i != -1 && i < index) {
                index = i;
                last = j;
            }
        }
        int[] array;
        if (last == -1) {
            array = null;
        } else {
            final int[] array2 = array = new int[2];
            array2[0] = last;
            array2[1] = index;
        }
        return array;
    }

    public static String substring(final String string, int fromIndex, int toIndex) {
        final int len = string.length();
        if (fromIndex < 0) {
            fromIndex += len;
            if (toIndex == 0) {
                toIndex = len;
            }
        }
        if (toIndex < 0) {
            toIndex += len;
        }
        if (fromIndex < 0) {
            fromIndex = 0;
        }
        if (toIndex > len) {
            toIndex = len;
        }
        if (fromIndex >= toIndex) {
            return "";
        }
        return string.substring(fromIndex, toIndex);
    }

    public static String toString(final Object value) {
        if (value == null) {
            return null;
        }
        return value.toString();
    }

    public static String toLowerCase(final String s) {
        return toLowerCase(s, null);
    }

    public static String toLowerCase(final String s, Locale locale) {
        if (s == null) {
            return null;
        }
        StringBuilder sb = null;
        for (int i = 0; i < s.length(); ++i) {
            final char c = s.charAt(i);
            if (c > '\u007f') {
                if (locale == null) {
                    locale = Locale.getDefault();
                }
                return s.toLowerCase(locale);
            }
            if (c >= 'A' && c <= 'Z') {
                if (sb == null) {
                    sb = new StringBuilder(s);
                }
                sb.setCharAt(i, (char) (c + ' '));
            }
        }
        if (sb == null) {
            return s;
        }
        return sb.toString();
    }
}

Related

  1. replace(char[][] arrays, char character, char[][] replacements)
  2. replaceAll(final Object[] objs, final String str)
  3. replaceAll(final String[] args, final String from, final String to)
  4. replaceAll(String src, String[] replace, String[] by)
  5. replaceChars(String s, char[] from, char[] to)
  6. replaceInArray(String[] thisArray, String findThis, String replaceWithThis)
  7. replaceWithValues(String subject, String needle, String[] values)