Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cap string to a certain length directly without a function

Tags:

c#

unity3d

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.

The answers on that question shows how to cap a string with a function 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.

Below is what I don't want to do:

string myString = "my long string";
myString = capString(myString, 20); //<-- Don't want to call a function each time

string capString(string strToCap, int strLen)
{
    ...
}

I was able to accomplish this with a property:

const int Max_Length = 20;
private string _userName;

public string userName
{
    get { return _userName; }
    set
    {
        _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 cap must have multiple variables defined for them. In this case, the _userName and the userName (property) variables.

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

like image 744
Programmer Avatar asked Dec 08 '16 20:12

Programmer


2 Answers

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.

like image 60
Serlite Avatar answered Nov 01 '22 05:11

Serlite


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;
like image 6
ispiro Avatar answered Nov 01 '22 04:11

ispiro