I want something similar to this question I found lead me to this answer on another question where I tried to convert it from php to c# my attempt to convert it... failed very badly:
private string trimString(string str, int maxCharacters = 16)
{
int textLength = str.Length;
return str.Substring(maxCharacters/2, textLength-maxCharacters).Insert(maxCharacters/2,"...");
}
So doing trimString("123456789", 9)
outputs 123...789
when I meant to do something like 123...012
or something similar.. I can't quite figure it out and I've tried many different versions mostly resulting in an input that is out of order.
If it's possible I'd like it to be a simple function and not include other libraries or initializing a new object every call as I plan on using this a lot in a quick session. I'd really like it not to cut off words and place the ellipses however this is not required.
The problem is that
Substring(maxCharacters/2, textLength-maxCharacters)
into which you insert the...
already has the characters that you don't want to see -456789
. There's no way to fix it after that.What you should do instead is to pick the prefix and the suffix, and join them together, like this:
Demo.