-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New API: String.Truncate(int maxLength, string truncationSuffix) #31655
Comments
Sometimes you don't want to remove the last part of the string, eg. it's a file path. The last part is the file name so shouldn't be removed. Most tools I've seen truncate somewhere in the middle of the string if it's a file path. |
Are there any localization concerns with such a feature? Examples would include special casing right to left languages or using a different default suffix per culture. |
Other issues that might need addressed:
Per your other open question:
This is already the case throughout the BCL. Even the |
I wonder how it applies to console UI. It's not that simple (considering wide characters), but the demand does exist. |
Should there be an option to truncate on a word boundary? It looks neater in many cases, provided of course it isn't too far back. |
@IanMercer Truncation at a word boundary would still suffer from two of the three problems I mentioned in my previous comment. But if we were determined to add it, we could call into ICU and use the logic at http://www.unicode.org/reports/tr29/ as a fallback. This makes me wonder if an out-of-band Unicode package might be appropriate. Could contain case folding (already in corefxlab), opinionated truncation, and a bunch of other things that we're hesitant to add to the BCL proper. @tarekgh? |
I am seeing the whole feature should be in he UI frameworks (e.g. WPF, WinForms and Xamarin) more than in the core framework. This is mainly will be used for UI and not really for string manipulation. This feature has to work with the word breaking and line breaking which can depend on the language of the strings too. Also, usually string truncation will be needed to fit in the UI controls (like line wrapping in the editors or browsers) which means is more about the size of the used font and the width of the UI control |
@GrabYourPitchforks Oh, I agree, done 'properly' this is not a simple feature, I was just adding fuel to that argument. I don't think this belongs in core. UI Frameworks can handle it for most cases where truncation is needed and for the occasional non-UI case, like logging, it's a trivial piece of code to write for your specific situation (e.g. English, UTF-8, spaces as breaks, ...). |
I propose a new method on
String
to perform a fairly common task: Truncating a string to a maximum length. Scenarios:In my experience, this is quite a common need. I often need to define a helper function for this.
Semantics:
"..."
.API:
Open question 1: What happens, when
maxLength < truncationSuffix.Length
and a truncation must occur? The API should never exceed the maximum length. This could be anArgumentException
or the suffix itself could be truncated. In my opinion, input validation should enforcemaxLength >= truncationSuffix.Length
. This should always be enforced for reasons of API simplicity, not just in the cases where it matters (truncation).An alternative view to that: What if the maximum length is dynamic? For example, when the user resizes a GUI window, maybe the maximum length is programmatically reduced. In that case, it could drop to below the ellipsis length and we'd like to truncate the ellipsis to meet the maximum length.
Open question 2: Should the empty string be normalized to
string.Empty
? I usually insert this optimization into string helpers that I write. Not sure if this is appropriate in the BCL.Open question 3: Allow
truncationSuffix == null
? If it is allowed, it must be equivalent to""
. But why would we allow two different inputs with the same semantics? Can callers not just use the default value""
, or specify""
explicitly? How is this handled in other places in the BCL? My opinion: Disallownull
. Let's be strict about input validation.Open question 4: Is there a better name for
truncationSuffix
? This name can never be changed for compatibility reasons. Better get it right. I did not likeellipsis
because a lot of people would now know what that word means. Instead ofSuffix
we could sayPostfix
.The text was updated successfully, but these errors were encountered: