3 ms·
May be not reversing, but trimming a Unicode string to certain character count is a close relative and it is a very common operation.
by abcd_f 13y ago
May be not reversing, but trimming a Unicode string to certain character count is a close relative and it is a very common operation.
- lmm 13y agoRight, but what's the count you want there? It's either a byte count or a grapheme cluster count. The .count() on most current languages' string types doesn't correspond to either of those, so isn't really useful.
- jeltz 13y agoWhat do you use it for? Unless you have a monospaced font the number of characters do not mean much. So unless you are implementing command line tools or text editors it should not be that common.
- ygra 13y agoEllipsisising text when it does not fit into a label, for example. And if you just remove code points from the end (instead of graphemes) until the string (including ellipsis) fits then you might just drop a diacritic.
- Pxtl 13y agoTruncating with ellipsis in the GUI in a desktop app. I can measure rendered length on a desktop, so I can truncate down to the desired number of pixels, round down to the nearest char, and then tack on "...". I would hate to see a semantically-important accent mark lost this way.
- ashashwat 13y agoYou have a search query and you want to remove stopwords and normalize the query.
- jerf 13y agoI have a database field limited to 100 "characters" [1]. The user sent me a form submission with 150. I need to do something to resolve that. This is incredibly common. Truncation to a defined size is routine. [1]: I'm leaving "characters" undefined here, because no matter what Unicode-aware definition you apply here, you've got trouble.
- jeltz 13y agoHave you checked how your database counts? Does it count code points or does it try to count graphemes? I assume the former, but I guess you would still have to cut the input at a grapheme border when truncating the input.
- deleted 13y ago[deleted]
- al2o3cr 13y ago"I have a database field limited to 100 "characters"." Well there's your problem right there... "The user sent me a form submission with 150. I need to do something to resolve that." Any software that defines "do something" here as "silently discard 1/3rd of the user input" is software I'm going to throw in the trash. If you must have fixed-length fields, surely telling the user "much characters, wow overflow" is better than just chopping the input.
- glhaynes 13y agoWhat if it's a batch ETL process where there is no "user" to tell that it went wrong? The point that when you're worrying about string length, it's often an indicator of a separate problem is a good one. But some things really do need the ability to measure/truncate strings and not every situation allows just throwing the software in the trash as an option.
- jerf 13y agoSince this seems to be confusing people, I'm providing a small hypothetical example here. "Any software that defines "do something" here as "silently discard 1/3rd of the user input" is software I'm going to throw in the trash." You are reading far more in than I put in. I merely said somehow you need to resolve this; you put a particular resolution in my mouth, then attacked. I did choose the web for one reason, which is that you can't avoid this case; you can try to limit the UI to generating 100 characters only (and I still haven't defined "characters"...), but it's 15 seconds for a user to pull open Firebug and smash 150 characters into your form submission anyhow. Somehow, you better resolve this, and as quickly as you mounted the high horse when faced with the prospect of mere truncation, throwing the entire request out for that will cause somebody else to mount an equally high horse....