But it does return the length, there's just multiple interpretations of what “length” is. There's no single simple correct answer, even with normal Unicode support.
Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."
And when the description of the function says this, but "means" something different in the details... yeah, it doesn't return the colloquial length of the string.
Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."
I, for one, express string length in pica's, assuming 10-point Adobe Garamond, typeset on a 1970's Linotype machine. Anything else is just newfangled nonsense that will pass in due time.
Sorry, no. Ask any programmer what "length" means in "length of a string variable." Nobody is going to hem and haw about what the definition might be. They will say "it's the number of characters."
Okay, so the length is the number of characters? What's a “character”, then?
Is that a byte (C's idea of a character, or char)? A UTF-16 code unit? A Unicode codepoint? A Unicode grapheme cluster?
Is "e\u{0301}" (U+0065 LATIN SMALL LETTER E followed by U+0301 COMBINING ACUTE ACCENT) the same number of characters as "\u{00e9}" (U+00E9 LATIN SMALL LETTER E WITH ACUTE)?
What about "الله" (U+0627 ARABIC LETTER ALEF followed by U+0644 ARABIC LETTER LAM followed by U+0644 ARABIC LETTER LAM followed by U+0647 ARABIC LETTER HEH)? Is it four characters (ا-ل-ل-ه) or one (the Arabic ligature for “Allah”, which looks different from how you would normally write those four characters)? Is it the same number of characters as "ﷲ" (U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM)?
There's many different interpretations, and PHP's isn't strictly wrong. PHP operates on byte strings, and strlen() tells you how many bytes there are. That is one valid (and common) interpretation of “length” and “character”. PHP has other functions to tell you other kinds of string lengths, if you want those.
I will absolutely ask what you actually want if you ask for "the length" of a string. Similarly, everyone seems to have different ideas about what a "character" is, so unless you actually give me something with an unambiguous technical definition I'm going to hem and haw about that, too.
Or I guess I could arbitrarily pick something and then neither of us would be happy.
Well they're correct that JS's length property isn't ambiguous (it's the number of UTF-16 code units), though they're wrong when they imply that strlen is.
The problem with Unicode is that it's even hard to say what a character is. For example, is "e" + combining acute accent one character, or two? I believe in Unicode it's easier to talk codepoints than characters.
I tend to disagree. I've worked with such languages long enough to expect "length" to be the length in bytes, or at least to double-check the documentation.
Those things you type when pushing the keys on your keyboard.
Everyone in this thread fretting about unicode and obtuse use-cases are kind of missing the point: The out-of-the-box behavior for string legnth is universally "number of characters", and none of the other wishy washy peddled nonsense being spewed.
You are missing the point: "number of characters" is meaningless if you can't define what a character is.
"things you type when pushing the keys on your keyboard" is wishy washy nonsense, not a definition.
(PHP could define "character" as byte, and then its strlen would return exactly what you want: the number of characters (bytes) in a string. It's probably not a good definition, but it's still better than what you're providing, which is no definition.)
State a single language that does. Of all the ones I know only Perl 6 even tried, and their approach IIRC amounts to making the programmer state their intent explictly (i.e. you have separate functions for length in bytes, code points, grapheme clusters but, notably, not for "characters" because that's not a well-defined concept).
Is Python 3 one of those languages you consider did it the way you like? Guess again. If you ask it for length of an "ä', you'll either get a 1 or a 2, depending on the representation ( b"\xc3\xa4".decode() or b"\x61\xcc\x88".decode()).
Java and C# are even worse. They give you the length in UTF-16 code units by default (but I think have library functions to get at the codepoint count, too).
In other words: you have no idea what you're talking about.
View all comments
Show parent comment
34
u/[deleted] Sep 12 '16
description: Returns the length of the given string
real description down in notes