count characters including spaces

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

count characters including spaces

Post by tsolignani » 14 Mar 2020 09:26

Good morning everyone.

Is there a way to count characters including spaces when using length or another function in automagic.

Sharing something to WhatsApp status, you face the 700 chars limits, and I suspect it includes spaces, for for instance it refuses to share a string which is 635 chars, according to automagic.

Sure I could lower the limit to something smaller but I would like to be more accurate.

Thank you, have a nice weekend.

Horschte
Posts: 56
Joined: 03 Nov 2014 18:00

Re: count characters including spaces

Post by Horschte » 14 Mar 2020 23:28

When you use length() your result is without spaces?
For me it's working fine:

Code: Select all

s = "my string";
r = length(s); //r: 9

User avatar
tsolignani
Posts: 187
Joined: 12 Jan 2019 11:53
Location: Vignola, Mo, Italy
Contact:

Re: count characters including spaces

Post by tsolignani » 17 Mar 2020 21:00

Thank you.

Then it should be WhatsApp's fault.

It kept telling me it was more than 700 characters, but it wasn't so I supposed WA was counting white spaces as well, while maybe it's its error message which is wrong (you can put less than 700).

Thank you.

User avatar
Desmanto
Posts: 2709
Joined: 21 Jul 2017 17:50

Re: count characters including spaces

Post by Desmanto » 18 Mar 2020 19:53

@tsolignani : This is most likely caused by the length() only count chars, while whatsapp or any other app limit the "chars" using byte storage. Non ASCII char usually use 2-4 bytes in one char. Example Japanese char usually use 3 bytes, emoji use 4 bytes. This is counted as 1 char only when using length().

I am still finding ways to calculate the real byte count. Have tried several java code and can't find a working one.
So far I use workaround code, using encodeURL(), find the non ASCII (and some symbol) using find(). Then use simple math to find the real length.

Code: Select all

status = "Hello ありがとう";
len = length(status); //11 chars, but wrong

//step by step
encode = encodeURL(status); //encode the space and japanese char
lenencode = length(encode); //total length of each encoded byte becomes 3, give 53
percent = length(findAll(encode, '%[0-9A-F]{2}')); //find the number of %xx encoded
reallen = lenencode - 2 * percent; //subtract by twice of that number
Hello = 5 chars, space = 1 char, ありがとう = 5 chars. Total is 11 chars, according to length(). However the byte used is not 11 byte.

encodeURL() and we get Hello%20%E3%81%82%E3%82%8A%E3%81%8C%E3%81%A8%E3%81%86
Which is total 53 chars. Space is now %20, become 3 chars (thrice). Each japanese char now become 9 chars, but it is actually 3 bytes only (since each %xx represent 1 byte). So we know that each encoded char expanded 3 times.
We find all the encoded char and count the occurance, result is 16 byte. 1 from space, 3 byte each from 5 japanese chars.
Since we need count the byte size only, we substract twice the length of the encoded char occurance, give us the real byte size = 21
Hello = 5 byte
space = 1 byte
ありがとう = 15 byte

I don't know how Whatsapp count the max status char limit, but maybe it count every byte used. Try this and see if you can post more than 700 byte. Adding emoji is one way to added up the byte used.


If you are OK with code above , then this is the single line version.

Code: Select all

status = "Hello ありがとう";
//single line version
reallen = length(encodeURL(status)) - 2 * length(findAll(encodeURL(status), '%[0-9A-F]{2}'));
Index of Automagic useful thread List of my other useful posts (and others')
Xiaomi Redmi Note 5 (whyred), AOSP Extended v6.7 build 20200310 Official, Android Pie 9.0, Rooted.

Post Reply