Why does std::string have a length() member function in addition to size()?

As per the documentation, these are just synonyms. size() is there to be consistent with other STL containers (like vector, map, etc.) and length() is to be consistent with most peoples’ intuitive notion of character strings. People usually talk about a word, sentence or paragraph’s length, not its size, so length() is there to make … Read more

Java substring: ‘String index out of range’

It is a pity that substring is not implemented in a way that handles short strings – like in other languages e.g. Python. Ok, we cannot change that and have to consider this edge case every time we use substr, instead of if-else clauses I would go for this shorter variant: myText.substring(0, Math.min(6, myText.length()))

DataAnnotations StringLength Attribute MVC – without max value

Is there a way to only specify only a minimum length? Perhaps another attribute i can use? Using the standard data annotation, No. You must specify the MaximumLength. Only the other parameters are optional. In such a case, I’d recommend something like this: [StringLength(int.MaxValue, MinimumLength = 7)] You can also use a Regex (regular expression) … Read more

java.util.UUID.randomUUID().toString() length

Does java.util.UUID.randomUUID().toString() length always equal to 36? Yes!! it is. A UUID actually a 128 bit value (2 long). To represent 128 bit into hex string there will be 128/4=32 char (each char is 4bit long). In string format it also contains 4 (-) that’s why the length is 36. Example: 54947df8-0e9e-4471-a2f9-9af509fb5889 32 hex char … Read more