Here you can find the source of collapse(String str, String characters, String replacement)
public static String collapse(String str, String characters, String replacement)
//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(); } }