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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -