Java String Remove removeAll(String targetStr, String removeStr)

Here you can find the source of removeAll(String targetStr, String removeStr)

Description

remove All

License

Apache License

Declaration

public static String removeAll(String targetStr, String removeStr) 

Method Source Code

//package com.java2s;
/*/*from   ww  w.  jav  a 2  s.  co m*/
 * Copyright 2014-2015 the original author or authors.
 *
 * 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.Collection;
import java.util.Iterator;

import java.util.Map;

import java.util.Set;

public class Main {
    public static String removeAll(String targetStr, String removeStr) {
        if (targetStr == null) {
            throw new IllegalArgumentException("'targetStr' is null");
        }
        if (removeStr == null) {
            throw new IllegalArgumentException("'start' is null");
        }
        return removeAll(targetStr, new String[] { removeStr });
    }

    public static String removeAll(String targetStr, String[] removeStrs) {
        if (targetStr == null) {
            throw new IllegalArgumentException("'targetStr' is null");
        }
        if (removeStrs == null) {
            throw new IllegalArgumentException("'removeStrs' is null");
        }
        for (int i = 0; i < removeStrs.length; i++) {
            String removeStr = removeStrs[i];
            if (removeStr == null) {
                throw new IllegalArgumentException(
                        "'removeStrs' has null: index==[" + i + "]");
            }
            targetStr = convertAll(targetStr, removeStr, "");
        }
        return targetStr;
    }

    public static String removeAll(String targetStr, String start,
            String end) {
        if (targetStr == null) {
            throw new IllegalArgumentException("'targetStr' is null");
        }
        if (start == null) {
            throw new IllegalArgumentException("'start' is null");
        }
        if (end == null) {
            throw new IllegalArgumentException("'end' is null");
        }
        int startIndex = 0;
        int endIndex = 0;
        StringBuffer sb = new StringBuffer(targetStr);
        while (true) {
            startIndex = sb.toString().indexOf(start);
            if (startIndex == -1) {
                break;
            }
            endIndex = sb.toString().indexOf(end, startIndex + 1);
            if (endIndex == -1) {
                break;
            }
            sb.delete(startIndex, endIndex + 1);
        }
        return sb.toString();
    }

    public static String convertAll(String targetStr, String oldStr,
            String newStr) {
        if (targetStr == null) {
            throw new IllegalArgumentException("'targetStr' is null");
        }
        if (oldStr == null) {
            throw new IllegalArgumentException("'oldStr' is null");
        }
        if (newStr == null) {
            throw new IllegalArgumentException("'newStr' is null");
        }

        String result = "";
        int index = 0;
        StringBuffer sb = new StringBuffer(targetStr);
        while (true) {
            index = sb.toString().indexOf(oldStr);
            if (index == -1) {
                result = result + sb.toString();
                break;
            }
            sb.delete(index, index + oldStr.length());
            sb.insert(index, newStr);
            int endIndex = index + newStr.length();
            result = result + sb.substring(0, endIndex);
            sb.delete(0, endIndex);
        }

        return result;
    }

    public static String convertAll(String targetStr,
            Map<String, String> convertMap) {
        if (targetStr == null) {
            throw new IllegalArgumentException("'targetStr' is null");
        }
        if (convertMap == null) {
            throw new IllegalArgumentException("'replaceMap' is null");
        }
        Set<String> keySet = convertMap.keySet();
        Collection<String> valCol = convertMap.values();
        Iterator<String> keyIte = keySet.iterator();
        Iterator<String> valIte = valCol.iterator();
        while (keyIte.hasNext()) {
            String oldStr = (String) keyIte.next();
            String newStr = (String) valIte.next();
            if (oldStr == null) {
                throw new IllegalArgumentException(
                        "'replaceMap' has null key!");
            }
            if (newStr == null) {
                newStr = "";
            }
            targetStr = convertAll(targetStr, oldStr, newStr);
        }
        return targetStr;
    }
}

Related

  1. getAndRemove(String key)
  2. parseAndRemoveDuplicates(String s, String del)
  3. removeAll(String origStr, String removeStr)
  4. removeAndConcatenateAlternate(String s1, String s2)
  5. removeBlankLines(String s)
  6. removeCommandFromString(String commandString)
  7. removeEmpties(final String... values)