0

I want something similar to this question I found lead me to this answer on another question where I tried to convert it from to 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.

Community
  • 1
  • 1
Codingale
  • 220
  • 6
  • 17
  • So you want to remove the characters from middle of string, how many characters you want to remove and do you want to replace them with `.` ? what about strings with even number of characters ? how would you define the middle then ? – Habib Jun 09 '15 at 14:46
  • So essentially you want first 3 and last 3 characters of string. Right? – Rahul Jun 09 '15 at 14:49
  • Yes. Similar to my old phone's function of replacing a long string with the start of it, trimming the middle, and leaving the end untouched. For example doing "Bob needs those ... by 12:30 on Monday" or similar. – Codingale Jun 09 '15 at 14:51
  • Mind someone explain why it was downvoted so I may improve for the future? I'd rather not create poor quality questions. – Codingale Jun 10 '15 at 02:47

2 Answers2

2

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:

private static string trimString(string str, int maxCharacters = 16) {
    if (str.Length <= maxCharacters) {
        return str;
    }
    var suffixLength = maxCharacters / 2;
    // When maxCharacters is odd, prefix will be longer by one character
    var prefixLength = maxCharacters - suffixLength;
    return string.Format(
        "{0}...{1}"
    ,   str.Substring(0, prefixLength)
    ,   str.Substring(str.Length-suffixLength, suffixLength)
    );
}

Demo.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Good improvement on checking str.Length against maxCharacters, since that optional argument has a default value. – Aaron Thomas Jun 09 '15 at 14:55
  • Though I like smaller functions such as the two line version you initially had this works majorly better although I'm a bit saddened that C# isn't quite as simple as PHP in this case. – Codingale Jun 10 '15 at 02:59
1

this returns 123...012 for trimString("123456789012", 6) the first and the last 3 characters, seperated with ....

public static string trimString(string str, int max = 16)
{
    if (str.Length <= max)
    {
        return str;
    }   
    return str.Substring(0, max / 2) + "..." + str.Substring(str.Length - max / 2, max / 2);
}
fubo
  • 44,811
  • 17
  • 103
  • 137
  • This is something like I had planned but couldn't quite do. I do like the fact it relies purely on substring and it should be a easy operation compared to the other function however in my instance it doesn't quite matter. – Codingale Jun 10 '15 at 03:01