Splitting string to array based on string length (amount of characters)

OPK

New member
Hi. Looks easy enough, but I've been unable to make this work. Some apps eg Google Translate, Twitter, SMS etc only allow a max amount of characters being entered. So how do I split a 500 characters string into chunks of maximal 200 characters, while keeping the last word intact?
I'm thinking
- use Regex to find last space before every 200th char
- Replace it with π
- Use split to array with π as delimiter
But none of the Regex formula's I found seems to work well...
 

OPK

New member
As said, I found several suggestions which, I couldn't get to work:
• ^\s*(?: (\S{200})|([\s\S]{1,200})(?!\S)) ->no space between ":" and ":" but if I delete that, it will show a smilie...
• .{1,200}\w*+
 
Last edited:

OPK

New member
.{1,200}\s is working fine on Regex 101 site; it is working fine in Extract Text + First Match, but it is not working in Remove Text (bug?) and as there is no First Match option; it is not the result I need...
 

RSF

Well-known member
Something like the attached -- using a regular expression to pick off 200-character chunks at a time, appending each to an array, and doing a straight (non-regex) match for the Remove Text action?
1707456473753.png
 

Attachments

  • Split_long_string_into_chunks.macro
    8.3 KB · Views: 4

OPK

New member
BIG thanks RSF, you did in 10 statements, what I did in 46; your macro is almost correct, as it misses the last chunk. It is however present in the str var, so I added one statement and now it is perfect.
1707512140554.png

Problem solved :)
Thanks again!
 
  • Like
Reactions: RSF

RSF

Well-known member
Ah, I see what's happening. Good catch. The issue arises due to the regular expression looking for a space (\s) after each chunk. My test string had a space at the end, so it worked without that extra action at the end; your version works perfectly for input strings with or without a trailing space, although it may end up with an unnecessarily-short final chunk.

An alternative would be to edit the initial action that assigns the AllText variable to the str variable, to append a space:
Screenshot 2024-02-10 8.04.28 AM.png
Then the original loop works OK, without that additional chunk assignment at the end...
 
Top