Extracting Data from Text with Truncate and Split By Functions
In this quick video, I will show you how to use the truncate and the split by functions in Bubble to extract and manipulate a longer string of text. I have an input field here with a long string of text and here are just some of the ways that I can extract different bits of data from it.
Using Truncate Function
I have a text field down here and I'm going to say input A's value and then let's say I just want the first 12 characters so I can use the truncate to 12 and then if I hit preview it will show me the first 12 characters. Now what if I wanted to do that from the end? Well Bubble also has a truncated from end which if I refresh now we just get the last 12 characters. If I go back to truncated from the start truncated 2 and refresh we'll see that this isn't so perfect because it's quite brutal to count by characters.
Using Split By Function
So what if I wanted to extract the first three words? Well, that's when I can use split by. Split by creates a list type in Bubble so if I say split by space and then I say items until 3 and then join with well let me just show you what this would look like to start with. This will list out the first three words but by default Bubble puts a comma between each one. In order to get rid of that comma I can use the join with space. So there we go I now have the first three words and if I wanted the first four words okay I can get the first four words.
Practical Applications
Those are just a couple of really handy tricks for reformatting the text data that your users may be putting into your app in order to get them in the format that you want. One example and this isn't going to be perfect would be first names and last names. I say it's not perfect because some people may have like a shorter yeah so they might have a shorter word in the middle like a muck for example followed by their surname but let me show you how this would work really well with first names and last names.
Extracting First and Last Names
So think of a first name last name I'll just put Tony Stark. So how would I extract just the first name? Well I can go with the split by a space and then I can just say first item and then if I wanted the last name I can go for last item. There you go that's just one example of how these little tricks knowing how to manipulate string data in Bubble can be really helpful.