Java String Shorten shorten(String pkg, boolean shorten)

Here you can find the source of shorten(String pkg, boolean shorten)

Description

shorten

License

Apache License

Parameter

Parameter Description
pkg a package name
shorten whether to shorten it or not

Return

the shortened name, in the form a.b.c.d.package

Declaration

public static String shorten(String pkg, boolean shorten) 

Method Source Code

//package com.java2s;
/**//from  w  w  w  . ja va  2  s.  c o  m
 * Copyright 1&1 Internet AG, https://github.com/1and1/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * @param pkg a package name
     * @param shorten whether to shorten it or not
     * @return the shortened name, in the form a.b.c.d.package
     */
    public static String shorten(String pkg, boolean shorten) {
        if (!shorten) {
            return pkg;
        }

        String[] parts = pkg.split("\\.");
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < parts.length - 1; i++) {
            result.append(parts[i].charAt(0));
            result.append(".");
        }
        result.append(parts[parts.length - 1]);
        return result.toString();
    }
}

Related

  1. shorten(String label, int maxLength)
  2. shorten(String line)
  3. shorten(String msg, int front, String join, int end)
  4. shorten(String name, int max)
  5. shorten(String nameForUI, int maxLen)
  6. shorten(String s)
  7. shorten(String s)
  8. shorten(String s)
  9. shorten(String s)