this post was submitted on 02 Oct 2023
0 points (NaN% liked)

Programming

19631 readers
249 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities [email protected]



founded 2 years ago
MODERATORS
top 2 comments
sorted by: hot top controversial new old
[โ€“] [email protected] 0 points 2 years ago* (last edited 2 years ago) (1 children)

I love the comparison of string length of the same UTF-8 string in four programming languages (only the last one is correct, by the way):

Python 3:

len("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ")

5

JavaScript / Java / C#:

"๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".length

7

Rust:

println!("{}", "๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".len());

17

Swift:

print("๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ".count)

1

[โ€“] [email protected] 1 points 2 years ago* (last edited 2 years ago)

That depends on your definition of correct lmao. Rust explicitly counts utf-8 scalar values, because that's the length of the raw bytes contained in the string. There are many times where that value is more useful than the grapheme count.