Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

truncate text using linq

Tags:

c#

linq

Looking for a simple query to truncate text by x amount of characters using Linq.

like image 254
FiveTools Avatar asked Dec 06 '25 20:12

FiveTools


1 Answers

Your question is unclear. Based on your comment to Justin's answer it sounds like the simpler way to achieve what you're describing would be as follows:

string input = "The quick brown fox jumped over the lazy dog";
string result = new String(input.Take(15).ToArray());
Console.WriteLine(result);

Notice that there is no need to call ToCharArray() since a string implements IEnumerable<char>. IntelliSense in VS2008 does not show up but the extension methods still work. Likewise, you can use the string constructor and pass it an array of characters instead of using Aggregate.

like image 106
Ahmad Mageed Avatar answered Dec 08 '25 09:12

Ahmad Mageed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!