c# - Limiting the maximum length of a string -
i need prevent string exceeding length and, if does, truncate last part of string.
i'm using gui.textfield
string user.
wrap property handle truncation:
public someclass { private const int maxlength = 20; // example private string _thestring; public string cappedstring { { return _thestring; } set { _thestring = value != null && value.length > maxlength ? value.substring(0, maxlength) : value; } } }
you can apply in whatever class needs implement it. carry on private
field, constant, , property cappedstring
.
Comments
Post a Comment