-1

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??

4

1 回答 1

1

从方法的JavaDoc:

允许您指定“左边缘”偏移。请注意,此左边缘不一定是结果中最左边的字符,也不一定是椭圆后面的第一个字符,但它会出现在结果中的某个位置。

在您找到的方法的代码中

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

你可以在这里阅读代码:http: //kickjava.com/src/org/apache/commons/lang/StringUtils.java.htm

所以你给出的两个例子都应该返回"abcdefg..."。你确定你有"abcdefgh"

于 2014-12-12T06:49:34.073 回答