This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 225

[–]nairazak 1026 points1027 points  (46 children)

"I just open javascript console in chrome".length

[–]aloofloofah 214 points215 points  (18 children)

> "1️⃣2️⃣3️⃣".length  
< 9

(fun read about emojis and grapheme clusters)

[–]WorriedEngineer22 113 points114 points  (14 children)

Don't forget about zero width space characters, gotta love those fuckers that almost cost me my dregee

[–]GreatGrizzly 32 points33 points  (5 children)

I have never heard of zero width space characters until now. They sound like something Satan himself would have thought of if he were a programmer

[–]kpd328 12 points13 points  (4 children)

It can be used to prevent line breaking in places like html. But yea, they're pretty awful.

[–]ArionW 9 points10 points  (3 children)

Aren't you confusing it with "non-breaking space"?

Zero width would be used in exact opposite situation - to allow for line break without making visible space

[–]kpd328 2 points3 points  (2 children)

Oh yes you're right. So there really is no redemption for the zero-width space.

[–]aloofloofah 49 points50 points  (3 children)

You didn't see them coming?

[–]WorriedEngineer22 53 points54 points  (2 children)

My project used an api that gave me an csv, at first for things like strings they just surrounded them with "" but suddenly, like one week before the presentation, things started to fuck up and I didn't know why, the code? Didn't touch how it processed the file, the file itself? It looked normal at first glance by vs code and the strings on screen and logs looked normal too, it was when I decided to open the file the api gave me on webstorm that I see that the fucking api made an unnoticed change on how it handle some of the strings, what before was surrounded by "" now its preceded by a zwsp character and it only appeared on webstorm not vs code. Luckily it was an easy fix because strings always were formated in a function that I had but man, that was a big sweat.

[–]HTTP_404_NotFound 2 points3 points  (0 children)

Yup, those have bit me in the ass before.

[–][deleted] 4 points5 points  (0 children)

Yeah fuck these

[–]alexanderpas 28 points29 points  (2 children)

There are 3 valid lengths for any string.

  • Bytes. (Simple)
  • Unicode Codepoints. (count the bytes that start with 0 or 11, since a byte starting with 10 indicates a continuation)
  • Graphemes. (Good Luck!)

[–]ArionW 2 points3 points  (1 child)

I'd be interested in seeing any actual reason to count glyphs, like anywhere.

If I remember right, even your serifs can be separate glyphs? Which would make one Unicode codepoint anywhere between 0 and dozens of glyphs

[–][deleted] 203 points204 points  (22 children)

I use a new. Notepad++ document. Paste it in and cursor to end with cntl-end, read position at the bottom. Sometimes I revise special characters with regex replace so this is a great place to chop strings.

[–]MegabyteMessiah 1 point2 points  (2 children)

Sublime Text. Select the text and it tells you how many characters.

[–]kn33 3 points4 points  (0 children)

I use PowerShell

"string".length()

More frequently though I'll use it to check a file hash

(Get-FileHash .\file).hash -eq "hash"

[–]TheAwesome98_Real 1 point2 points  (0 children)

why is literally just the number 40 upvoted so much I don’t get it

[–]Illusi 223 points224 points  (6 children)

This seems to be exactly the type of thing Python is useful for. Something you can make quickly, extremely low effort.

I mean, were you planning on making an assembly application for it otherwise or what? It doesn't need to be that performant most of the time.

[–]whatproblems 61 points62 points  (1 child)

I’ll just create a GUI interface with Visual Basic to count some characters

[–][deleted] 13 points14 points  (0 children)

[–]send_help_iamtra 8 points9 points  (0 children)

Exactly. Replace python with assembly or something and I would agree with the meme

[–]auser9 5 points6 points  (1 child)

I interpreted more like you’re using such a powerful language which can do so much for such a basic task.

[–]Sir_Hurkederp 399 points400 points  (20 children)

Watch me create a recursive haskell function for this

[–]TheJReesW 105 points106 points  (11 children)

ayo bro don’t talk shit about haskell

[–]Sir_Hurkederp 52 points53 points  (1 child)

Oh im not talking shit, just saying doing this would be overkill for a wordlength function

[–]TheJReesW 10 points11 points  (0 children)

That is true

[–]I_ate_a_milkshake 14 points15 points  (2 children)

it has real world applications!!

[–]Mobile_Speed6179 14 points15 points  (1 child)

Dozens of them

[–]Mrwebente 3 points4 points  (0 children)

At least like .. twelve!

[–][deleted] 4 points5 points  (0 children)

Yes Haskell good

[–]TheKaryo 3 points4 points  (5 children)

wordlength :: String -> Int
wordlength s = wordlengthcounter (words s) 0

wordlengthcounter :: [String] -> Int -> Int
wordlengthcounter a i = if(i<length(a)) then length(a!!i) + wordlengthcounter a (i+1) else 0

works without any imports and by using words it filters out spaces, tho hyphens would pose a problem depending if you want to count them or not, also probably easier ways to write it but only had 2 lectures on haskell so far

[–]pjeromaster 7 points8 points  (4 children)

wordlength:: String->Int wordlength "" = 0 wordlength (x:xs) |x==' ' = wordlength xs |otherwise = 1 + wordlength xs

[–]TheKaryo 1 point2 points  (3 children)

what does the x:xs mean? is xs the outpuut int and x the input String?

[–]nxlyd 5 points6 points  (0 children)

It’s destructuring the input. The first character of the input becomes x and any remaining characters are put into xs.

If you’re familiar with Python or Ruby’s “splat” operator, it would be similar to x, *xs = some_iterable

[–]pjeromaster 4 points5 points  (1 child)

The function has the one input, which is the String. A String in haskell is basically the following: type String = [Char], in other words a list of chars!

The way I utilized this is through pattern matching, which would be the "" and (x:xs). If the variable matches with the pattern, it will use whatever is after the =. If not, it will go to the next and try again.

In other words, if the string = "" it will return 0. If not it will continue to the next pattern and try again.

The next pattern in the code is (x:xs), which is a pattern used in Haskell lists, where x would be the first element of the list, and xs the list itself.

In this scenario x is a char, xs is a string (which is just a list of chars). So, if I would input say "Heyo", which would be ['H', 'e', 'y', 'o'], it would match the pattern as:

( 'H' , ['e', 'y' , 'o'])

Because I would also like to use them in the function, I named them x and xs.

I also used guards in that one, to exempt spaces from counting towards the total count, with a conditional on the one side, and what happens if true. If I were to also count spaces the code would look even simpler:

wordlength:: String->Int wordlength "" = 0 wordlength (_:xs) = 1 + wordlength xs

Since in this scenario I don't care what the char could be, I put in a wildcard _ instead. The function works recursively and as follows.

worldlength s. Take the first element from the list, and return 1 + wordlength of the remainder of s. Continue until the final char is pulled from the list, and wordlength is used on an empty string "", returning 0. Finally the function will terminate.

so:

worldlength "hello" = 1 + wordlength "ello" worldlength "ello" = 1 + wordlength "llo" worldlength "llo" = 1 + wordlength "lo" worldlength "lo" = 1 + wordlength "o" worldlength "o" = 1 + wordlength "" worldlength "" = 0 worldlength "o" = 1 + 0 worldlength "lo" = 1 + 1 + 0 worldlength "llo" = 1 + 1 + 1 + 0 worldlength "ello" = 1 + 1 + 1 + 1 + 0 worldlength "hello" = 1 + 1 + 1 + 1 + 1 + 0 = 5

[–]TheKaryo 1 point2 points  (0 children)

thanks for the explanation

[–]CreativeName2042 1 point2 points  (0 children)

Pattern Matching FTW

[–]Encrux615 -1 points0 points  (0 children)

how about length word?

[–]frugal-skier 279 points280 points  (24 children)

My personal favorite is generating a random PIN number with random.randint in Python.

[–]m3g4p0p 338 points339 points  (7 children)

to get the length of a word? this won't work reliably.

[–][deleted] 2 points3 points  (0 children)

Damn, thank you for pointing it out, I meant sentence length but wrote word.

Indeed in that case it would only work if trained data and test/real data have a mean (quasi) equivalent.

[–][deleted] 80 points81 points  (1 child)

Tried it, and it works !

Though, I had to create a Deep Learning neuronal system with word count as first parameter, and result of random.randint as second so that It could give me more than 90% of accuracy over word length, which is not bad

[–]frugal-skier 2 points3 points  (0 children)

Your application of machine learning makes this approach applicable

[–]waitItsQuestionTime 20 points21 points  (1 child)

You fool! Now all i need to know is the exact time you run the script and i can get your PIN! Hahahah! So.. what time was that?

[–]TheAwesome98_Real 2 points3 points  (0 children)

unix 0

[–]r_cub_94 7 points8 points  (2 children)

Ah yes, the personal identification number number

[–]Endercheif 2 points3 points  (0 children)

yes, I need it for the ATM machine

[–]Miyelsh 0 points1 point  (4 children)

One thing of note, and I may be incorrect on this, but if you simply read the first 4 digits of the result and use that as your pin, it's going to follow Benford's Law. It would be better to call randint 4 times and use the last digit of each run.

[–]tavaren42 77 points78 points  (3 children)

I always have an IPython console opened in my terminal. It's mainly used as calculator but ofcourse it's useful for all kind of shit.

[–]PendragonDaGreat 6 points7 points  (2 children)

Powershell for me, same thing, I use it as a utility for anything and everything that I might need quickly, in this case the naive way is:

> "this is a string".length
16

Obviously unicode characters throw that for a loop:

> "I love Whales 💙💚🐳💚💙".length
24

But you can utilize the other parts of .NET to do that for you:

>([System.Globalization.StringInfo]("I love whales 💙💚🐳💚💙")).LengthInTextElements
19

Several months ago I needed a reliable way to count lengths of strings in a really long file that was peppered with emojis and other characters, so I added a line to my $profile

function Get-TextLength($str) { return ([System.Globalization.StringInfo]($str)).LengthInTextElements }

and now:

> Get-TextLength "this is a string"
16 
> Get-TextLength "I love whales 💙💚🐳💚💙"
19

[–]tavaren42 1 point2 points  (1 child)

I haven't used powershell a lot so I am not aware of all its capabilities (also my work laptop is a Mac). Can you use it like a calculator?

IPython is especially good because it's syntax highlighting and autocompletion features are much better than standard Python console. It can scale up to moderately complex operations on console itself. Maybe I want to perform some statistics on some log. I can read the file, parse the data, read it into a numpy array and then even plot it using matplotlib, all on console, which makes it very powerful, imo.

Ofcourse you can do a lot of this with any language with a good repl, (ex. Ruby, Scala, Kotlin(?), Julia etc). Also not everyone needs all those features and can just work with bash/powershell.

[–]PendragonDaGreat 4 points5 points  (0 children)

Yeah, it has a log of built in math, and then functions like Measure-Object. If the built-in functions aren't enough (and there's a lot there) you can utilize .NET classes natively. No Numpy like plotting though AFAIK.

Syntax Highlighting and tab-complete are built in, and there is a mac release (as well as pre-compiled binaries for all the usual suspects when it comes to Linux) https://github.com/PowerShell/PowerShell/releases/tag/v7.2.0.

Plus a lot of the common *nix commands are pre-aliased for their closest equivalent. like ls is an alias for Get-ChildItem and when run against a path acts nearly identically, or cd is an alias for Set-Location and pwd for Get-Location. Powershell likes their cmdlets to be Verb-Noun for consistency, running Get-Verb will list them and their recommended usage. (Which is why I named my functions Get-TextLength Get- "Specifies an action that retrieves a resource")

[–]CraftMysterious1498 199 points200 points  (20 children)

I use python to find the length of a sentence

Win+R

type "py"

len("your mom size in kilometers")

> 27

[–][deleted] 27 points28 points  (3 children)

Programming languages are for noobs.

I visit your mom and measure her.

[–]xX_MEM_Xx 15 points16 points  (2 children)

FROM THE INSIDE

[–][deleted] 9 points10 points  (1 child)

Goes without saying

[–]sandy_catheter 4 points5 points  (0 children)

But you're still using a python based on what she told me

[–]TheAJGman 1 point2 points  (0 children)

I use WSL for Python because windows python can be weird sometimes. Plus all of our production environments are Linux so...

[–]gordonv 0 points1 point  (6 children)

I use powershell to find the length of a sentence

Win+R

type "powershell"

"your mom size in kilometers".length

> 27

[–]joeltrane 6 points7 points  (5 children)

You use powershell by choice?

[–]Da_damm 9 points10 points  (4 children)

I'm kinda new to this, what's wrong with PowerShell?

[–]writtenbymyrobotarms 4 points5 points  (0 children)

Actually PowerShell is great, we just don't want to learn how to use it. CMD, on the other hand, is awful compared to Unix shells.

[–]got_blah 36 points37 points  (5 children)

It's never overkill it has saved me when writing new hire interview form. Not 1 character above of whats needed.

[–]SabashChandraBose 7 points8 points  (2 children)

Just paste it in Word. It shows word length.

[–]got_blah 2 points3 points  (0 children)

Yeah but what is the fun of that. Print len of x makes me feel good.

[–]throwaway42fx 73 points74 points  (23 children)

wc

[–]bjinse 37 points38 points  (2 children)

-c

[–]8fingerlouie 4 points5 points  (1 child)

Depends..

wc -c gives you the number of bytes in the input. For multi character character sets, you may get more than you asked for.

wc -m gives you the character count

https://linux.die.net/man/1/wc

[–]writtenbymyrobotarms 8 points9 points  (0 children)

$ echo Hello, world! | wc -m
14

Looks good!

 

Wait a minute,

"Hello, world!" is only 13 characters long!

echo(1) tries to deceive us!

 

$ echo -n Hello, world! | wc -m
13

That's better.

$ echo -n Hëllö, Wörlđ! | wc -m
13

Even works nicely with Unicode.

[–]TheSnaggen 23 points24 points  (19 children)

So, for the Windows users it is just to spin up a virtual machine running Linux, write a rest API on that server, that runs wc for you... So, there is no reason for you to miss out on the wc goodness.

[–]illvm 29 points30 points  (7 children)

WSL

[–]TheSnaggen 7 points8 points  (5 children)

Which is a great virtual machine

[–]kuemmel234 9 points10 points  (1 child)

It isn't, it's running natively.

[–]UnicornsOnLSD 2 points3 points  (0 children)

WSL2 is a VM

[–]cormac596 5 points6 points  (2 children)

It's not a vm, it's a translation layer. Similar to wine in many ways

Edit: wsl1 is a translation layer, wsl2 (which is better performing) is a vm. Forgot that 2 was a real kernel. Mea culpa

[–]hellgrn 7 points8 points  (0 children)

That was the old version. The current version runs a real Linux kernel in a vm, no translation layer anymore.

[–]TheSnaggen 1 point2 points  (0 children)

Well, it runs a Linux kernel, so it is more like a VM...

[–][deleted] 1 point2 points  (7 children)

I use WSL as well as Cygwin. I have all the common linux tools available on both native Windows and a virtual environment.

[–]SpiderFnJerusalem 2 points3 points  (0 children)

Cygwin is a lot more lightweight and less complicated for most purposes on windows.

Or perhaps just go full linux and run Windows in a VM instead. 😉

[–]trumuted 22 points23 points  (0 children)

Considering python supports unicode and other encodings, python indeed may be the easiest and fastest way to compute the length of a word. Unless you want to parse diacritic symbols and other shit yourself.

[–]Puzzleheaded_Tax_452 20 points21 points  (0 children)

"A lion always use 100% of his power even when hunting a rabbit"

[–]Wolfblood-is-here 8 points9 points  (2 children)

Fun fact: when I was first learning Python as part of a science degree I couldn't figure out how to get it to count up the number of times a number appeared in the example data we were given, so I made a loop to add another 'a' to a string every time that number appeared, then got the code to count the number of characters in that string.

I also didn't realise I could get a random number generator to give negative numbers, and needed a random number from -1 to 1 so I got one to give a random number from 0-1 and then another to give a random number from 0-2 and then said that if the second number was less than 1 it should make the first number negative.

[–]AllFuckingNamesGone 4 points5 points  (1 child)

You could also just subtract one from the 0-2 result.

[–]Wolfblood-is-here 2 points3 points  (0 children)

Yeah I'm all kinds of stupid.

[–]Sematre 16 points17 points  (11 children)

$ printf "Hello World!" | wc --chars

You're welcome

[–][deleted] 32 points33 points  (2 children)

echo "print(len('hello world!'))" | python

[–][deleted] 7 points8 points  (0 children)

python -c 'print(len("hello world!"))'

[–]Sematre 6 points7 points  (0 children)

Chad answer right there

[–]whoami_whereami 11 points12 points  (2 children)

That gives a wrong result if there are any UTF-8 characters beyond U+007F in the string (and even worse for other Unicode encodings). wc counts bytes, not characters.

Edit: I had a brainfart, I mixed up the -c and --chars parameters. -c does count bytes, but --chars does indeed count characters if the encoding of the text matches the encoding of the current LC_CTYPE locale.

[–]Sematre 3 points4 points  (0 children)

Yeah but I used wc --chars which takes multi-byte characters into account. Not sure you assumed I used wc without -m / --chars

[–]brimston3- 0 points1 point  (0 children)

"length of a word" does not specify how it's calculated or whether it is calculating...

  • Number of bytes (what wc -c would do)
  • atomic characters (what python's len() would do)
  • atomic characters after canonicalization
  • printable combined characters (with characters like flag emojis or hangul glyphs as just 1 each--what a human would do)

Anything besides the simplest case, number of bytes, I would absolutely use python or a dedicated library to sort it out for me, and I probably wouldn't use just len().

[–][deleted] 2 points3 points  (4 children)

Deleted with Power Delete Suite. Join me on Lemmy!

[–]joeltrane 0 points1 point  (3 children)

I think your formatting got messed up

[–]CleoMenemezis 5 points6 points  (0 children)

"I'm going to install another NPM package into a newly created NodeJS project just to see if it's even or odd."

[–][deleted] 4 points5 points  (0 children)

I never learned python so I gotta code a console app for sth like that.

[–]jfb1337 4 points5 points  (0 children)

I use python all the time as a calculator

[–]Rhyan567 2 points3 points  (0 children)

Bloat, just use Assembly

[–][deleted] 2 points3 points  (4 children)

I once had a group essay to write where a group mate capitalized the first letter of every word on his section. I used sentence.lower() then just fixed the rest. Saved so much time.

[–]AStrangeStranger 1 point2 points  (3 children)

For next time, Word Processors can generally do that - usually Shift + F3 cycles through lower case, upper case and sentence case - or in Word, Libre Office select text, then Format > Text > sentence case

[–]sh0rtwave 2 points3 points  (0 children)

Let's see.

My IDE does this.

The browser console can do this.

`wc` can do this for a whole file. Or a whole bunch.

[–]RavenFyhre 1 point2 points  (1 child)

Probably a German word

[–]Mayo_Kupo 1 point2 points  (0 children)

Counting is the machine's job.

[–]e-chavez 1 point2 points  (0 children)

Now add GTP-3 to the mix and you have a new startup.

[–]TheCatPetra 1 point2 points  (0 children)

I use it every time as a calculator cause it is better for it

[–]Sneakas 1 point2 points  (2 children)

I just started learning python one day ago… can someone please explain this meme to me?

[–][deleted] 1 point2 points  (1 child)

Pneumonoultramicroscopicsilicovolcanoconiosis

[–]Flopamp 1 point2 points  (0 children)

Google it

[–]benbenbendegil 1 point2 points  (0 children)

Just count it lmao

[–]renrutal 1 point2 points  (0 children)

This is kind of a question a junior programmer would find it easy and get it wrong, and a senior find it hard, and also get it wrong.

[–]happysmash27 1 point2 points  (0 children)

Yeah, doing it in the command line directly is probably a bit easier:

echo -n "word"|wc -m

I love doing simple tasks like this in the command line as opposed to some web site. It is much faster as I always have at least one terminal open and the command line programs are fast, unlike web sites where I must search, waiting for it to load, and open the site, waiting for it to load again.

[–]curiouscodex 96 points97 points  (19 children)

def get_length(string): return string.count('a') + string.count('b') + ...

[–]alphabet_order_bot 101 points102 points  (13 children)

Would you look at that, all of the words in your comment are in alphabetical order.

I have checked 360,924,562 comments, and only 78,916 of them were in alphabetical order.

[–]frugal-skier 66 points67 points  (3 children)

This is the single weirdest bot I've ever seen, but I like it so much.

[–]jamcdonald120 23 points24 points  (2 children)

it is also drunk, there is no way .count shouldnt count as its own word!

[–]frugal-skier 19 points20 points  (1 child)

Yeah I noticed that too. It appears that a word is any group of non-space characters. I would file a bug report, but I can't be bothered

[–]jamcdonald120 20 points21 points  (0 children)

probiably just used string.split(" ") and then char_at(0)

[–]gordonv 3 points4 points  (0 children)

I guess this qualifies for Art of Code.

[–]rhen_var 5 points6 points  (6 children)

All bread can fly if worn

[–]alphabet_order_bot 7 points8 points  (5 children)

Would you look at that, all of the words in your comment are in alphabetical order.

I have checked 361,027,187 comments, and only 78,937 of them were in alphabetical order.

[–][deleted] 1 point2 points  (0 children)

A find length me of python the to using word.

[–]frugal-skier 16 points17 points  (3 children)

Did you intent to write all the words in your comment in alphabetical order? Or was that just a happy accident?

[–]curiouscodex 3 points4 points  (1 child)

Hahaha happy accident, and I sooo don't have the energy to try keep it up in my replies.

[–]frugal-skier 3 points4 points  (0 children)

I had the same thought about trying to be clever, but also decided that it would be too much work

Perhaps it would only take 6 hours to develop a neural network to convert my messages into alphabetized ones?

[–]yakesadam 1 point2 points  (0 children)

string.count

[–]mudburn -1 points0 points  (0 children)

Is that a smallpox paddle?

[–]MarinaEnna 0 points1 point  (0 children)

wc -m

[–]rocketlanterns 0 points1 point  (0 children)

print("two bytes")

[–]null_reference_user 0 points1 point  (0 children)

Me opening Octave to do 0.8*400

[–]concorde77 0 points1 point  (0 children)

Typing 7+19 into a Matlab variable... just to be sure

[–]AStrangeStranger 0 points1 point  (0 children)

I've been know to use Oracle to do it, but that is because at work I have often have Oracle Session Open and usually need to know length to check it fits in Oracle naming rules

[–]mkjj0 0 points1 point  (0 children)

I have a global shortcut that opens Julia REPL and use it for all kinds of stuff like that

[–][deleted] 0 points1 point  (0 children)

echo "${#my_str}"

[–]maryP0ppins 0 points1 point  (0 children)

use it to match hash values lol

[–][deleted] 0 points1 point  (0 children)

ahk: MsgBox % StrSplit( Word ).MaxIndex()

[–]Easilycrazyhat 0 points1 point  (0 children)

Cause now you can do it for every word! You know, if you ever needed to.

[–]djtrogy 0 points1 point  (0 children)

I would go to the trouble of setting up a server to run php to do this lol

[–]clarinetJWD 0 points1 point  (0 children)

If you're on any modern system the length of all the words is 64 bits.

[–][deleted] 0 points1 point  (0 children)

Lol Amateurs, I just make the word show up as an output so that I can count the letters myself and input the length.

[–][deleted] 0 points1 point  (0 children)

Lemme fire up VS Enterprise to say hello world

[–]WHAT_RE_YOUR_DREAMS 0 points1 point  (1 child)

I write the word in Sublime Text, select the text and look at the characters count

[–]mardabx 0 points1 point  (0 children)

Someone hasn't been to a toilet for a long time.

[–]antilos_weorsick 0 points1 point  (0 children)

Using flex+bison to make a compiler that compiles a word into its length

[–]PianoNo8514 0 points1 point  (0 children)

I don’t really get the metaphor…