I want to reduce the length of a string. Like...
This string...
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
Becomes this string...
Lorem ipsum dolor sit amet, consectetur adipiscing...
How do I do this? Thanks!
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
string small_foo = foo.SubString(0, 50);
Using Substring to get the first 50 characters of the string. http://msdn.microsoft.com/en-us/library/aka44szs.aspx
Code
public string Truncate(string input, int length)
{
if (input.Length < length) return input;
int index = input.IndexOf(' ', length);
return input.Substring(0, index) + "...";
}
...
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
string bar = Truncate(foo, 50);
Console.WriteLine(bar);
Output
Lorem ipsum dolor sit amet, consectetur adipiscing...
Instead of
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With