Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the length of a string to 150 characters?

Tags:

c#

I tried the following:

var a = description.Substring(0, 150);

However this gives a problem if the length of description is less than 150 chars. So is there another way I can limit the length to 150 that won't give an error when string length is for example 20.

like image 991
Barcino Avatar asked Jul 26 '11 16:07

Barcino


People also ask

How do you shorten the length of a string?

Make a loop at the end of the string. After cutting the string at the proper length, take the end of the string and tie a knot at the very end, then fold the string over and tie a loop, about the same size as the original loop (about 2cm in diameter).

How do you restrict length in regex?

The ‹ ^ › and ‹ $ › anchors ensure that the regex matches the entire subject string; otherwise, it could match 10 characters within longer text. The ‹ [A-Z] › character class matches any single uppercase character from A to Z, and the interval quantifier ‹ {1,10} › repeats the character class from 1 to 10 times.

Is there a limit to string length?

While an individual quoted string cannot be longer than 2048 bytes, a string literal of roughly 65535 bytes can be constructed by concatenating strings.


1 Answers

var a = description == null 
        ? string.Empty 
        : description.Substring(0, Math.Min(150, description.Length));
like image 173
ChaosPandion Avatar answered Oct 20 '22 18:10

ChaosPandion