Java String Replace strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple)

Here you can find the source of strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple)

Description

Old version of strReplace.

License

Apache License

Parameter

Parameter Description
strText a parameter
strFrom a parameter
strTo a parameter
bMultiple a parameter

Return

Converted string

Declaration

public static String strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * //from  ww  w. ja  v  a 2  s.  c o m
 * 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.
 ******************************************************************************/

import java.util.ArrayList;

import java.util.List;

public class Main {
    /**
     * Old version of strReplace. No longer used.
     * 
     * @deprecated
     * @param strText
     * @param strFrom
     * @param strTo
     * @param bMultiple
     * @return Converted string
     */
    public static String strReplaceOld(String strText, String strFrom, String strTo, boolean bMultiple) {

        int iFromLength = strFrom.length();
        int iToLength = strTo.length();

        int iOccurs = 0;
        String strOutput = new String(strText);

        iOccurs = strOutput.toLowerCase().indexOf(strFrom.toLowerCase(), 0);

        int iStartWith = 0;

        while (iOccurs != -1) {
            strOutput = strOutput.substring(0, iOccurs) + strTo + strOutput.substring(iOccurs + iFromLength);
            iStartWith = (iOccurs - 1) + iToLength + 1;

            if (bMultiple)
                iOccurs = strOutput.toLowerCase().indexOf(strFrom.toLowerCase(), iStartWith);
            else
                iOccurs = -1;
        }

        return strOutput;
    }

    /**
     * Creates a new list that contains the same elements than the parameter
     * list, but with all string elements converted to lower case
     * 
     * @param listOriginal
     * @return The list with all strings converted to lower case
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    public static List toLowerCase(List listOriginal) {

        List list = new ArrayList();
        Object elem;
        for (int i = 0; i < listOriginal.size(); i++) {
            elem = listOriginal.get(i);
            if (elem instanceof String) {
                list.add(((String) elem).toLowerCase());
            } else {
                list.add(elem);
            }
        }

        return list;

    }
}

Related

  1. replaceSign(String sourceStr, char startc, char endc, String replaceStr)
  2. replaceSpace(String str, int actualLength)
  3. replaceUnsafeNamespaceDelimiters(String input)
  4. replaceWildcards(String wildcardPattern)
  5. searchAndReplace(String source, String search, String replace)
  6. subStitute(String value, String pattern, String replacement)
  7. substituteIgnoreCase(String s, String pattern, String replacement)