dnrajugade / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Truncate a String at a maximum length #1194

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
We're considering adding a new API (likely in Strings) to truncate a String at 
a specified maximum length, optionally including a caller specified truncation 
indicator at the end if it is truncated (e.g. "..."). It should avoid splitting 
surrogate pairs. We don't intend for it to do anything that relates to i18n, 
such as truncating on a word boundary.

Would this be useful? Does anyone have a use case for wanting to truncate the 
beginning or middle of a String rather than the end? Post any thoughts here.

Original issue reported on code.google.com by cgdecker@google.com on 6 Nov 2012 at 8:28

GoogleCodeExporter commented 9 years ago
At some point next year, I'd like to work on a library that creates display 
strings for URLs by hiding "http:," by unencoding percent escapes, and by 
truncating -- maybe at the end, maybe in the middle, as I think Chrome makes 
its choice adaptively.

Original comment by cpov...@google.com on 6 Nov 2012 at 8:33

GoogleCodeExporter commented 9 years ago
Would definitely be useful for us. We actually have a Strings2 class with 
exactly one method, truncate. It's very simple, just truncates after max chars

Original comment by toellr...@gmail.com on 6 Nov 2012 at 8:54

GoogleCodeExporter commented 9 years ago
Of course a method like that is always nice to have in a standard library, but 
I'd say it would only really be useful when truncating on a word boundary. That 
wouldn't have to be i18n-savvy either, because it could work with a 
client-supplied CharMatcher to figure out what the word boundaries are.

Original comment by SeanPFl...@googlemail.com on 6 Nov 2012 at 9:02

GoogleCodeExporter commented 9 years ago
Word boundaries are much more complicated than a CharMatcher could handle.

Original comment by kevinb@google.com on 7 Nov 2012 at 1:39

GoogleCodeExporter commented 9 years ago
If support for a truncation indicator is implemented, then I'd like to ask for 
the size of said indicator to be taken into consideration, perhaps optionally. 
That is, I think truncating a string S to size N using an indicator I of size M 
should yield the string S[0:N-M] + I. This serves two purposes: 

1. When the available space is precisely N characters (as might be the case 
when using using a fixed-width font), one can just provide N and I. No need to 
compute (N - I.length()).

2. This avoids situations in which one drops the last (say) two characters of a 
string, only to replace them with the thee-letter string '...'. (That is, 
information was dropped without any clear benefit.)

Original comment by stephan...@gmail.com on 7 Nov 2012 at 8:28

GoogleCodeExporter commented 9 years ago
@stephan202: Yes, this method will truncate the string to be at most N 
characters including the truncation indicator. So truncate("foobar", 6, "...") 
returns "foobar" while truncate("foobar2", 6, "...") yields "foo...". As such, 
it'll be illegal to pass in a truncation indicator that's longer than N.

Original comment by cgdecker@google.com on 7 Nov 2012 at 4:43

GoogleCodeExporter commented 9 years ago
Great! :)

Original comment by stephan...@gmail.com on 7 Nov 2012 at 4:45

GoogleCodeExporter commented 9 years ago
I would appreciate it.

>>Does anyone have a use case for wanting to truncate the beginning or middle 
of a String rather than the end?

Truncate in beginning: Maybe for RTL languages? 

http://en.wikipedia.org/wiki/Right-to-left

Not sure about that thought.

Original comment by sebastia...@gmail.com on 25 Mar 2013 at 3:02

GoogleCodeExporter commented 9 years ago
I recently removed Apache commons-lang from my project in favour of Guava. The 
StringUtils.abbreviate method was one of the last things which I had to port at 
source level into project's utils package. So I would appreciate truncate 
method in Guava too, either in simple API or in more complicate yet general API 
aka Splitter/Joiner.

@sebastian practical use case for truncating the beginning is file paths - the 
"interesting thing" is at the end (file name) whereas uninteresting directories 
near the root are at the beginning.

Original comment by tomas.za...@gmail.com on 26 Mar 2013 at 7:45

GoogleCodeExporter commented 9 years ago
Let's discuss this @ API review...

Original comment by kak@google.com on 22 Aug 2013 at 11:25

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Chrome puts ... in the middle (see title bar).

org.eclipse.jface.dialogs.Dialog#shortenText puts ... in the middle.

http://help.eclipse.org/indigo/index.jsp?topic=%2Forg.eclipse.platform.doc.isv%2
Freference%2Fapi%2Forg%2Feclipse%2Fjface%2Fdialogs%2FDialog.html

http://dev.eclipse.org/viewsvn/viewvc.cgi/org.eclipse.jface/src/org/eclipse/jfac
e/dialogs/Dialog.java?view=markup

Original comment by sebastia...@gmail.com on 10 Sep 2013 at 5:45

GoogleCodeExporter commented 9 years ago
This ticket should be labeled `Milestone-Release16`, I think, as the upcoming 
release includes Ascii#truncate.

http://docs.guava-libraries.googlecode.com/git-history/v16.0-rc1/javadoc/com/goo
gle/common/base/Ascii.html#truncate%28java.lang.CharSequence,%20int,%20java.lang
.String%29

Original comment by stephan...@gmail.com on 6 Jan 2014 at 9:23

GoogleCodeExporter commented 9 years ago
Good catch, forgot to update this. The API we added only truncates at the end 
of the input and requires that the string used to indicate truncation be 
specified. It explicitly doesn't handle any i18n or even Unicode related issues 
such as avoiding splitting surrogate pairs or combining characters, which is 
why it's in the Ascii class. It's mostly for very basic truncation needs, such 
as for truncating debugging strings to a reasonable length.

Original comment by cgdecker@google.com on 6 Jan 2014 at 9:29

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:13

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:08