How does the offset parameter works in the function abbreviate(String str,int offset,int maxWidth), in package org.apache.commons.lang.StringUtils

384 views Asked by At

I am presently working with the apache commons lang package, StringUtils class. I found there are two abbreviation methods: abbreviate(String str,int maxwidth) and abbreviate(String str,int offset,int maxwidth) it is absolutely ok with the first one. But when come to the second one it is little bit confusing and I really need the clarification. I saw two cases of the abbreviate(String str,int offset,int maxwidth) function. those are:-

abbreviate("abcdefghijklmno",1,10)

returns "abcdefg...", and the second:

abbreviate("abcdefghijklmno",4,10)

also returns "abcdefg...".

After seeing this I am really in confution how exactly the offset parameter works??

1

There are 1 answers

2
Thomas Stets On BEST ANSWER

From the JavaDoc of the method:

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.

In the code of the method you find

if (offset <= 4) {
   return str.substring(0, maxWidth - 3) + "...";
}

You can read the code here: http://kickjava.com/src/org/apache/commons/lang/StringUtils.java.htm

So both examples you give should return "abcdefg...". Are you sure you got "abcdefgh" ?