Java String Extract extract(Properties p, String prefix)

Here you can find the source of extract(Properties p, String prefix)

Description

Returns another Properties object only containing the keys from the this object that start with the specified prefix.

License

Open Source License

Parameter

Parameter Description
p the properties to extract the subset from.
prefix the prefix of the keys to extract

Return

the new object; or null if no keys with such prefix were found; returns the input object if prefix if null or empty

Declaration

public static Properties extract(Properties p, String prefix) 

Method Source Code

//package com.java2s;
/*/*from www.  j  a  v a  2s. c om*/
* Copyright (c) 2000 - 2005 Nokia Corporation and/or its subsidiary(-ies).
* All rights reserved.
* This component and the accompanying materials are made available
* under the terms of "Eclipse Public License v1.0"
* which accompanies this distribution, and is available
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
*
* Initial Contributors:
* Nokia Corporation - initial contribution.
*
* Contributors:
*
* Description:  
*
*/

import java.util.Map;

import java.util.Iterator;
import java.util.Properties;

public class Main {
    /**
     * Returns another {@link Properties} object only containing the keys
     * from the this object that start with the specified prefix. The keys
     * in the returned properties object are subkeys of the keys in the
     * original object with the prefix stripped off.
     *
     * @param p the properties to extract the subset from.
     * @param prefix the prefix of the keys to extract
     * @return the new object; or null if no keys with such prefix were found;
     *         returns the input object if prefix if null or empty
     */
    public static Properties extract(Properties p, String prefix) {

        Properties subset = null;

        // return self if prefix is null or empty
        if (prefix == null || prefix.length() == 0) {
            if (p.size() > 0)
                subset = p;

            // extract the subset
        } else if (!p.isEmpty()) {
            Iterator itr = p.entrySet().iterator();
            int length = prefix.length();

            /* enumerate the keys and find the matching entries */
            while (itr.hasNext()) {
                Map.Entry entry = (Map.Entry) itr.next();
                String key = (String) entry.getKey();
                if (key.startsWith(prefix)) {

                    // create the subset when we first time get here
                    if (subset == null) {
                        subset = new Properties();
                    }

                    // add key-value pair to the subset
                    String value = (String) entry.getValue();
                    subset.put(key.substring(length), value);
                }
            }
        }

        return subset;
    }
}

Related

  1. extract(final String grammarString, final char separator)
  2. extract_tokens(String in_string, String delimiters)
  3. extractAggregationPrimKeysFromSql( final String sql)
  4. extractArrayData(String input, StringBuilder output)
  5. extractArrayData(String input, StringBuilder output)