Cap string to a certain length directly without a function


Not a duplicate of this.

I want to make a string have a max length. It should never pass this length. Lets say a 20 char length. If the provided string is > 20, take the first 20 string and discard the rest.

This answers shows how to do this but I want to do it directly without a function. I want the string length check to happen each time the string is written to.

I was able to do it with property:

const int Max_Length = 20;
private string _userName;

public string userName
    get { return _userName; }
        _userName = string.IsNullOrEmpty(value) ? "" : value.Substring(0, Max_Length);

Then I can easily use it whout calling a function to cap it:

userName = "Programmer";

The problem with this is that every string I want to have a max char must have multiple variables defined for them.

Any clever way of doing this without creating multiple variables for each string and without having to call a function each time I want to modify the string?

Show source
| c#   | unity3d   2016-12-08 21:12 2 Answers

Answers ( 2 )

  1. 2016-12-08 22:12

    Create a class with a string property, and put all of that code there. Then, you can use s.Value anywhere as a string with the needed characteristic.

    Something like:

    class Superstring
        int max_Length = 20;
        string theString;
        public Superstring() { }
        public Superstring(int maxLength) { max_Length = maxLength; }
        public Superstring(string initialValue) { Value = initialValue; }
        public Superstring(int maxLength, string initialValue) { max_Length = maxLength; Value = initialValue; }
        public string Value { get { return theString; } set { theString = string.IsNullOrEmpty(value) ? value : value.Substring(0, Math.Min(max_Length, value.Length)); } }

    and use:

    Superstring s = new Superstring("z");
    s.Value = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz";
    string s2 = s.Value;
  2. 2016-12-08 22:12

    Interesting situation - I would suggest creating a struct and then defining an implicit conversion operator for it, similar to what was done in this Stack Overflow question.

    public struct CappedString
        int Max_Length;
        string val;
        public CappedString(string str, int maxLength = 20)
            Max_Length = maxLength;
            val = (string.IsNullOrEmpty(str)) ? "" :
                  (str.Length <= Max_Length) ? str : str.Substring(0, Max_Length);
        // From string to CappedString
        public static implicit operator CappedString(string str)
            return new CappedString(str);
        // From CappedString to string
        public static implicit operator string(CappedString str)
            return str.val;
        // To making using Debug.Log() more convenient
        public override string ToString()
            return val;
        // Then overload the rest of your operators for other common string operations

    Later you can use it like so:

    // Implicitly convert string to CappedString
    CappedString cappedString = "newString";
    // Implicitly convert CappedString to string
    string normalString = cappedString;
    // Initialize with non-default max length
    CappedString cappedString30 = new CappedString("newString", 30);

    Note: This isn't perfect solution, unfortunately - because the implicit conversion doesn't give a way to transfer existing values to the new instance, any CappedString initialized with a non-default length value will need to be assigned to using the constructor, or its length limit will revert back to its default.

◀ Go back