LINQ to Objects supports queries on string objects but when I use code such as below:
string SomeText = "this is some text in a string";
return SomeText.Take(6).ToString();
All I get is:
System.Linq.Enumerable+<TakeIterator>d__3a`1[System.Char]
This is discussed as an “accident” in this question but this is what I am actually trying to do and I cannot find it through search anywhere.
I know there are other ways to manipulate strings but then I also know you can do some really cool tricks with LINQ and I just would like to know if there is a way to trim a string to a certain length with LINQ?
There’s no method built in to System.Linq to do this, but you could write your own extension method fairly easily:
Unfortunately, because
object.ToStringexists on all .NET objects, you would have to give the method a different name so that the compiler will invoke your extension method, not the built-inToString.As per your comment below, it’s good to question whether this is the right approach. Because
Stringexposes a lot of functionality through its public methods, I would implement this method as an extension onStringitself:You would use it as follows:
This has the advantage of not creating any temporary arrays/objects when the string is already shorter than the desired length.