Java tutorial
//package com.java2s; /******************************************************************************* * Copyright (c) 2006, 2012 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ import java.util.HashSet; public class Main { /** * @param base * @param set * @param title */ private static void compareTitleWithBase(String base, boolean bracketed, HashSet<Integer> set, String title) { // Check to see it the name starts with the prefix if (title.toLowerCase().startsWith(base.toLowerCase())) { // with brackets add on is: space, (, #, ) int minSizeNumAddOn = 4; if (!bracketed) // without brackets and space add on is just number minSizeNumAddOn = 1; // We found a possible auto-generated name // Determine number if (title.length() >= (base.length() + minSizeNumAddOn)) { String numPart; if (bracketed && title.charAt(base.length()) == ' ') { // We skipped the space since we already checked numPart = title.substring(base.length() + 1); } else if (!bracketed) { // without brackets, the numPart is everything after the prefix numPart = title.substring(base.length()); } else { // We are using brackets and there was no space return; } if (bracketed) { if (numPart.charAt(0) == '(') { // We are using brackets and confirmed that the open bracket exists // move on to just the number part numPart = numPart.substring(1); } else { // We are using brackets and there is no opening bracket return; } } // We found an auto-generated name StringBuffer buffer = new StringBuffer(); // Parse the number between the brackets for (int j = 0; j < numPart.length(); j++) { char current = numPart.charAt(j); // Make sure its a digit if (Character.isDigit(current)) { buffer.append(current); } else { if (!bracketed || numPart.charAt(j) != ')' || j != numPart.length() - 1) { // without brackets, a non digits means this will not conflict // with brackets, anything other than a ')' means this will not conflict // with brackets, if this is not the last character it will not conflict return; } // if all conditions passed, this is the last loop, no need to break } } // Convert the number we found into an actual number if (buffer.length() > 0) { set.add(new Integer(buffer.toString())); } } else { // No number to parse // Assume it is just base set.add(new Integer(0)); } } } }