Abbreviates a String using ellipses.
abbreviate(null, *) = null abbreviate("", 4) = "" abbreviate("abcdefg", 6) = "abc..." abbreviate("abcdefg", 7) = "abcdefg" abbreviate("abcdefg", 8) = "abcdefg" abbreviate("abcdefg", 4) = "a..." abbreviate("abcdefg", 3) = IllegalArgumentException
public class Main { public static void main(String[] argv) throws Exception { String str = "demo2s.com"; int maxWidth = 6; System.out.println(abbreviate(str, maxWidth)); }//w ww . ja v a2 s .co m public static final String EMPTY = ""; public static String abbreviate(String str, int maxWidth) { return abbreviate(str, 0, maxWidth); } /** * <p>Abbreviates a String using ellipses. This will turn * "Now is the time for all good men" into "...is the time for..."</p> * * <p>Works like <code>abbreviate(String, int)</code>, but allows you to specify * a "left edge" offset. Note that this left edge is not necessarily going to * be the leftmost character in the result, or the first character following the * ellipses, but it will appear somewhere in the result. * * <p>In no case will it return a String of length greater than * <code>maxWidth</code>.</p> * * <pre> * abbreviate(null, *, *) = null * abbreviate("", 0, 4) = "" * abbreviate("abcdefghijklmno", -1, 10) = "abcdefg..." * abbreviate("abcdefghijklmno", 0, 10) = "abcdefg..." * abbreviate("abcdefghijklmno", 1, 10) = "abcdefg..." * abbreviate("abcdefghijklmno", 4, 10) = "abcdefg..." * abbreviate("abcdefghijklmno", 5, 10) = "...fghi..." * abbreviate("abcdefghijklmno", 6, 10) = "...ghij..." * abbreviate("abcdefghijklmno", 8, 10) = "...ijklmno" * abbreviate("abcdefghijklmno", 10, 10) = "...ijklmno" * abbreviate("abcdefghijklmno", 12, 10) = "...ijklmno" * abbreviate("abcdefghij", 0, 3) = IllegalArgumentException * abbreviate("abcdefghij", 5, 6) = IllegalArgumentException * </pre> * * @param str the String to check, may be null * @param offset left edge of source String * @param maxWidth maximum length of result String, must be at least 4 * @return abbreviated String, <code>null</code> if null String input * @throws IllegalArgumentException if the width is too small * @since 2.0 */ public static String abbreviate(String str, int offset, int maxWidth) { if (str == null) { return null; } if (maxWidth < 4) { throw new IllegalArgumentException("Minimum abbreviation width is 4"); } if (str.length() <= maxWidth) { return str; } if (offset > str.length()) { offset = str.length(); } if ((str.length() - offset) < (maxWidth - 3)) { offset = str.length() - (maxWidth - 3); } if (offset <= 4) { return str.substring(0, maxWidth - 3) + "..."; } if (maxWidth < 7) { throw new IllegalArgumentException("Minimum abbreviation width with offset is 7"); } if ((offset + (maxWidth - 3)) < str.length()) { return "..." + abbreviate(str.substring(offset), maxWidth - 3); } return "..." + str.substring(str.length() - (maxWidth - 3)); } } /* * 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. */
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String s = "www.demo2s.com"; int len = 2; System.out.println(abbr(s, len)); }/*from ww w . ja v a2s . co m*/ public static String abbr(String s, int len) { int length = length(s); if (length <= len) { return s; } return s.substring(0, len - 3) + "..."; } public static int length(String s) { return s != null ? s.length() : 0; } }
//package com.demo2s; public class Main { public static void main(String[] argv) throws Exception { String str = "www.demo2s.com"; int maxWidth = 6; System.out.println(abbreviate(str, maxWidth)); }/*from ww w . j ava 2s . co m*/ public static String abbreviate(String str, int offset, int maxWidth) { if (str == null) { return null; } if (maxWidth < 4) { throw new IllegalArgumentException("Minimum abbreviation width is 4"); } if (str.length() <= maxWidth) { return str; } if (offset > str.length()) { offset = str.length(); } if ((str.length() - offset) < (maxWidth - 3)) { offset = str.length() - (maxWidth - 3); } if (offset <= 4) { return str.substring(0, maxWidth - 3) + "..."; } if (maxWidth < 7) { throw new IllegalArgumentException("Minimum abbreviation width with offset is 7"); } if ((offset + (maxWidth - 3)) < str.length()) { return "..." + abbreviate(str.substring(offset), maxWidth - 3); } return "..." + str.substring(str.length() - (maxWidth - 3)); } public static String abbreviate(String str, int maxWidth) { return abbreviate(str, 0, maxWidth); } }