Skip to main content

Timeline for Get string truncated to max length

Current License: CC BY-SA 3.0

6 events
when toggle format what by license comment
May 18, 2020 at 7:05 comment added Matt Your answer is still valid, and I agree, null-checking operators where introduced late in C#. The accepted answer does not check for maxLength < 0, and I like the fail fast approach you mentioned. So my hint was only for the null-checking. By the way, I am looking for a truncate parameter for the .ToString() method, because I think truncating after .ToString() is inefficient. Do you have an idea how that can be achieved? Recall that .ToString() can be invoked on every object or primitive type.
May 16, 2020 at 13:55 comment added Konrad Morawski My answer was posted 5 years ago, I'm not sure if C# 6.0 (which introduced the null-safety operator) had been released back then yet ;) Obviously I agree that's the way to go now - as in the accepted answer
May 15, 2020 at 12:47 comment added Matt Correct! Good catch. One small improvement: Remove the null-checking if statement, then modify the return statement as follows: return value?.Substring(0, Math.Min(value.Length, maxLength));; will do the same, but much shorter. :-)
Aug 16, 2015 at 0:38 history edited Konrad Morawski CC BY-SA 3.0
added 2 characters in body
Aug 15, 2015 at 20:54 history edited Konrad Morawski CC BY-SA 3.0
added 1 character in body
Aug 15, 2015 at 20:21 history answered Konrad Morawski CC BY-SA 3.0