Java String Collapse collapse(String str, String characters, String replacement)

Here you can find the source of collapse(String str, String characters, String replacement)

Description

collapse

License

Open Source License

Declaration

public static String collapse(String str, String characters, String replacement) 

Method Source Code

//package com.java2s;
/*//from  w  w w.  j  av  a  2  s  . co m
 *  ?????? : ???? ???? ??? ??
 *  ??? : MOB ??? ???
 *
 *  Copyright 2007 by Digital Solution Center, Samsung Electronics, Inc.,
 *   All rights reserved.
 *
 *  This software is the confidential and proprietary information
 *  of Samsung Electronics, Inc. ("Confidential Information").  You
 *  shall not disclose such Confidential Information and shall use
 *  it only in accordance with the terms of the license agreement
 *  you entered into with Samsung.
 *
 *
 */

public class Main {

    public static String collapse(String str, String characters, String replacement) {
        if (str == null) {
            return null;
        }

        StringBuffer newStr = new StringBuffer();

        boolean prevCharMatched = false;
        char c;
        for (int i = 0; i < str.length(); i++) {
            c = str.charAt(i);
            if (characters.indexOf(c) != -1) {
                // this character is matched
                if (prevCharMatched) {
                    // apparently a string of matched chars, so don't append anything
                    // to the string
                    continue;
                }
                prevCharMatched = true;
                newStr.append(replacement);
            } else {
                prevCharMatched = false;
                newStr.append(c);
            }
        }
        return newStr.toString();
    }
}

Related

  1. collapse(char c)
  2. collapse(double[] w)
  3. collapse(float[][] array)
  4. collapse(String t)
  5. collapse(String text)
  6. collapsedName(Class klass)
  7. collapseLines(String message)