Java tutorial
//package com.java2s; import android.net.Uri; import java.util.List; public class Main { /** * Returns a name for a new dictionary based on import URI. */ static String generateDictionaryNameByUri(Uri importUri, List<String> dictionaryNameList) { String name = importUri.getLastPathSegment(); // Strip extension { int index = name.lastIndexOf('.'); if (index > 0) { // Keep file path beginning with '.'. name = name.substring(0, index); } } if (!dictionaryNameList.contains(name)) { // No-dupped dictionary name. return name; } // The names extracted from uri is dupped. So look for alternative names by adding // number suffix, such as "pathname (1)". int suffix = 1; while (true) { String candidate = name + " (" + suffix + ")"; if (!dictionaryNameList.contains(candidate)) { return candidate; } // The limit of the number of dictionaries is much smaller than Integer.MAX_VALUE, // so this while loop should stop eventually. ++suffix; } } }