How do I get the first 250 words of a string?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You need to split the string. You can use the overload without parameter(whitespaces are assumed).
Note that you need to add
using System.LinqforEnumerable.Take.You can use
ToList()orToArray()ro create a new collection from the query or save memory and enumerate it directly:Update
Since it seems to be quite popular I’m adding following extension which is more efficient than the
Enumerable.Takeapproach and also returns a collection instead of the (deferred executed) query.It uses
String.Splitwhere white-space characters are assumed to be the delimiters if the separator parameter is null or contains no characters. But the method also allows to pass different delimiters:It can be used easily: