S
shapper
Hello,
I created a string extension to trim a string and add a "..." at the
end if the string was trimmed.
I am getting an error:
Exception Details: System.ArgumentOutOfRangeException: Index was out
of range. Must be non-negative and less than the size of the
collection.
Parameter name: startIndex
On code line:
Int32 i = trimmed.LastIndexOfAny(punctuation, length);
What am I doing wrong?
// ---------- Begin Code ----------
public static void Run() {
String url = "http://domain.com/article/1";
String title = "Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. ";
String trimmed = String.Format("{0} {1}", title.Trim(true, 138 -
url.Length), url);
} // Run
public static String Trim(this String value, Boolean ellipsis,
Int32 length) {
// Remove initial empty spaces
value.Trim();
// Check inputs
if (length == 0)
if (ellipsis)
throw new ArgumentOutOfRangeException();
else
return String.Empty;
if (value.Length < length) return value;
// Trim
String trimmed = value.Substring(0, ellipsis ? length - 1 :
length);
Char[] punctuation = { ' ', ',', '.', '?', '!', ':', ';', '-' };
Int32 i = trimmed.LastIndexOfAny(punctuation, length);
if (i != -1)
trimmed = trimmed.Substring(0, i);
return ellipsis ? trimmed + '\x2026' : trimmed;
} // Trim
// ---------- End Code ----------
Thanks,
Miguel
I created a string extension to trim a string and add a "..." at the
end if the string was trimmed.
I am getting an error:
Exception Details: System.ArgumentOutOfRangeException: Index was out
of range. Must be non-negative and less than the size of the
collection.
Parameter name: startIndex
On code line:
Int32 i = trimmed.LastIndexOfAny(punctuation, length);
What am I doing wrong?
// ---------- Begin Code ----------
public static void Run() {
String url = "http://domain.com/article/1";
String title = "Lorem Ipsum is simply dummy text of the printing
and typesetting industry. Lorem Ipsum has been the industry's standard
dummy text ever since the 1500s, when an unknown printer took a galley
of type and scrambled it to make a type specimen book. ";
String trimmed = String.Format("{0} {1}", title.Trim(true, 138 -
url.Length), url);
} // Run
public static String Trim(this String value, Boolean ellipsis,
Int32 length) {
// Remove initial empty spaces
value.Trim();
// Check inputs
if (length == 0)
if (ellipsis)
throw new ArgumentOutOfRangeException();
else
return String.Empty;
if (value.Length < length) return value;
// Trim
String trimmed = value.Substring(0, ellipsis ? length - 1 :
length);
Char[] punctuation = { ' ', ',', '.', '?', '!', ':', ';', '-' };
Int32 i = trimmed.LastIndexOfAny(punctuation, length);
if (i != -1)
trimmed = trimmed.Substring(0, i);
return ellipsis ? trimmed + '\x2026' : trimmed;
} // Trim
// ---------- End Code ----------
Thanks,
Miguel