How can I separate strings into substrings by characters positions?

Suppose I have this text in a multiline-input saved in the database:

“Lorem ipsum pain amet, consectetur adipiscing elit. Donec placerat mi exx, vehicula ultricies turpis auctor eget. Pellentesque ac fermentum ligula. Cras lectus est, hendrerit rhoncus augue vel, rhoncus lobortis lorem. ornare quis, vulputate et ex. Praesent aliquet tortor ante elementum rutrum eros fermentum eget. Cras lorem urna, ultricies vel elit a, dignissim efficitur nibh.”

What I’m looking for is to separate that string into different parts, depending on the number of characters.

from 0 to 100: “Lorem ipsum dolor amet, consectetur adipiscing elit. Donec placerat mi exx, vehicula ultricies elits”

from 101 to 201: " turpis auctor eget. Pellentesque ac fermentum ligula. Cras lectus est, hendrerit rhoncus augue vel,"

from 202 to 345: "rhoncus lobortis lorem. Pellentesque vel porttitor quam. Proin libero quam, dapibus a ornare quis, vulputate et ex. Praesent aliquet tortor ante "

from 346 to end: “elementum rutrum eros fermentum eget. Cras lorem urna, ultricies vel elita, dignissim efficitur nibh.”

  • :truncated to and :truncated from end to are not useful to achieve that
  • :split by is also not helpful
  • :extract with Regex I tried, but did not achieve it; not sure if it is possible with regex

Any idea how to achieve this goal?

Thanks,

Hi @gon.guty :wave:

Does the amount of letters of your substrings will be constant?

Why not?

You can try JavaScript as well…

let text = "Lorem ipsum pain amet, consectetur adipiscing elit. Donec placerat mi exx, vehicula ultricies turpis auctor eget. Pellentesque ac fermentum ligula. Cras lectus est, hendrerit rhoncus augue vel, rhoncus lobortis lorem. ornare quis, vulputate et ex. Praesent aliquet tortor ante elementum rutrum eros fermentum eget. Cras lorem urna, ultricies vel elit a, dignissim efficitur nibh."

let substring1 = text.substring(0, 101);
let substring2 = text.substring(101, 202);
let substring3 = text.substring(202, 346);
1 Like

:truncated to and :truncated from end to are definitely useful to achieve this!!..

Check this regular expression-

const result = text.match(/.{1,50}/g);

it will return list of text with 50 char of length.

Ankur@Nocodetalks
Looking for a Bubble Coach? Buy Bubble.io mentorship

1 Like

Thanks all of you. I will try regex and javascript.

I think :truncated to and :truncated from end does not work becase it only allows to select text from the start of the string to some position, or from the end of the string to some position; but it does not allow to select text in the middle.

I am missing something?

This is the behavior i understand happening

I think :truncated to and :truncated from end does not work becase it only allows to select text from the start of the string to some position, or from the end of the string to some position; but it does not allow to select text in the middle.
I am missing something?

Yes, you are missing something…

to truncate to something in the middle you need to use both truncated from end, and truncated to (in that order)…

e.g. to get the characters from 200-399 out of a 1000 character text, just use :truncated from end to (number of characters - 800): truncated to (200)

If you need to get multiple segments, and the numbers are variable, just use a list of numbers and use ‘format as text’ to truncate a different section of the text for each number in the list.

(although to be honest, the JavaScript option is probably simpler.)

1 Like

Thanks, all the answers were helpful.

I’m solving it with javascript, it’s almost done!

2 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.