Search code examples
c#regexdevexpressmaskedtextboxmaskedinput

Limit of characters in string


I have the textbox that takes some string. This string could be very long. I want to limit displayed text (for example to 10 characters) and attach 3 dots like:

if text box takes value "To be, or not to be, that is the question:" it display only "To be, or..."

Or

if text box takes value "To be" it display "To be"

            Html.DevExpress().TextBox(
                    tbsettings =>
                    {
                        tbsettings.Name = "tbNameEdit";;
                        tbsettings.Width = 400;
                        tbsettings.Properties.DisplayFormatString=???
                    }).Bind(DataBinder.Eval(product, "ReportName")).GetHtml();

Solution

  • If you need to use a regex, you can do this:

    Regex.Replace(input, "(?<=^.{10}).*", "...");
    

    This replaces any text after the tenth character with three dots.

    The (?<=expr) is a lookbehind. It means that expr must be matched (but not consumed) in order for the rest of the match to be successful. If there are fewer than ten characters in the input, no replacement is performed.

    Here is a demo on ideone.