Java String Sub String substractPrefixPostfix(Object obj, String prefix, String suffix)

Here you can find the source of substractPrefixPostfix(Object obj, String prefix, String suffix)

Description

This class reflectively looks for the given postfix and removes it from the classname of the given object.

License

Open Source License

Declaration

public static String substractPrefixPostfix(Object obj, String prefix,
        String suffix) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Formal Mind GmbH and University of Dusseldorf.
 * /*from w ww. jav  a  2  s .c om*/
 * 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:
 *     Michael Jastram - initial API and implementation
 ******************************************************************************/

public class Main {
    /**
     * This class reflectively looks for the given postfix and removes it from
     * the classname of the given object. Should the result contain camel case,
     * then spaces will be inserted.
     * <p>
     * 
     * If obj is itself a {@link Class}, its simple name is used directly.
     * 
     * If the postfix does not match, the simple class name is returned.
     * <p>
     * 
     * If obj is null, the empty string is returned.
     * <p>
     * 
     * The idea is that in some places, it is convenient to extract information
     * directly from the CamelCased classname, e.g. SpecRelationTypeItemProvider
     * => "Spec Relation Type".
     */
    public static String substractPrefixPostfix(Object obj, String prefix,
            String suffix) {
        if (obj == null) {
            return "";
        }

        String className = obj instanceof Class ? ((Class<?>) obj)
                .getSimpleName() : obj.getClass().getSimpleName();
        if (!className.startsWith(suffix) && !className.endsWith(suffix)) {
            return className;
        }
        String name = className.substring(prefix.length(),
                className.length() - suffix.length());
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (i != 0 && Character.isUpperCase(c)) {
                sb.append(' ');
            }
            sb.append(c);
        }
        return sb.toString();
    }
}

Related

  1. substract(double[] a, double[] b)
  2. substract(double[] a, double[] b)
  3. substract(int[] array1, int[] array2)
  4. substract(Number n1, Number n2)
  5. substraction2(double[] a, double[] b)
  6. subStrBeforeDotNotIncludeDot(String str)
  7. subStrByBytes(String str, int len, String tail)
  8. substrByLength(String str, int start, int size, String markCode)
  9. subStrBytes2(String str, int byteLength)