Here you can find the source of shortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength)
public static String shortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength)
//package com.java2s; /******************************************************************************* * Copyright (c) 1998, 2015 Oracle and/or its affiliates. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0 * which accompanies this distribution.//from w w w . j av a 2 s. c o m * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * * Contributors: * Oracle - initial API and implementation from Oracle TopLink * dminsky - added countOccurrencesOf(Object, List) API * 08/23/2010-2.2 Michael O'Brien * - 323043: application.xml module ordering may cause weaving not to occur causing an NPE. * warn if expected "_persistence_*_vh" method not found * instead of throwing NPE during deploy validation. ******************************************************************************/ public class Main { /** * Returns a String which is a concatenation of two string which have had enough * vowels removed from them so that the sum of the sized of the two strings is less than * or equal to the specified size. */ public static String shortenStringsByRemovingVowelsToFit(String s1, String s2, int maximumStringLength) { int size = s1.length() + s2.length(); if (size <= maximumStringLength) { return s1 + s2; } // Remove the necessary number of characters int s1Size = s1.length(); int s2Size = s2.length(); StringBuilder buf1 = new StringBuilder(); StringBuilder buf2 = new StringBuilder(); int numberOfCharsToBeRemoved = size - maximumStringLength; int s1Index = 0; int s2Index = 0; int modulo2 = 0; // While we still want to remove characters, and not both string are done. while ((numberOfCharsToBeRemoved > 0) && !((s1Index >= s1Size) && (s2Index >= s2Size))) { if ((modulo2 % 2) == 0) { // Remove from s1 if (s1Index < s1Size) { if (isVowel(s1.charAt(s1Index))) { numberOfCharsToBeRemoved--; } else { buf1.append(s1.charAt(s1Index)); } s1Index++; } } else { // Remove from s2 if (s2Index < s2Size) { if (isVowel(s2.charAt(s2Index))) { numberOfCharsToBeRemoved--; } else { buf2.append(s2.charAt(s2Index)); } s2Index++; } } modulo2++; } // Append the rest of the character that were not parsed through. // Is it quicker to build a substring and append that? while (s1Index < s1Size) { buf1.append(s1.charAt(s1Index)); s1Index++; } while (s2Index < s2Size) { buf2.append(s2.charAt(s2Index)); s2Index++; } // return buf1.toString() + buf2.toString(); } /** * Returns true if the character given is a vowel. I.e. one of a,e,i,o,u,A,E,I,O,U. */ public static boolean isVowel(char c) { return (c == 'A') || (c == 'a') || (c == 'e') || (c == 'E') || (c == 'i') || (c == 'I') || (c == 'o') || (c == 'O') || (c == 'u') || (c == 'U'); } }