Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 451 Vote(s) - 3.46 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Substring() - ArgumentOutOfRangeException

#11
description.Substring(0, Math.Min(description.Length, 240));
Reply

#12
Let's try to keep this simple...

We only need to truncate to a given max length, so how about we call it what it is:

description.TruncateTo(240);

The extension method that enables the above (ellipsis is appended by default if truncated):

public static class StringExtensions
{
public static string TruncateTo(this string val, int maxLength, bool ellipsis = true)
{
if (val == null || val.Length <= maxLength)
{
return val;
}

ellipsis = ellipsis && maxLength >= 3;
return ellipsis ? val.Substring(0, maxLength - 3) + "..." : val.Substring(0, maxLength);
}
}

Reply

#13
Using .Net 5 or higher.

I recommend using:

yourText.Substring(0, Math.Min(yourText.Length, 100));

This logic will use the smallest value between the comparisons, as it returns the smallest of two numbers. As per [MS documentation][1].


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through