Tips for working with text

Join Text

The join block allows you to stick two or more pieces of text together.
join

note: Programmers often refer to pieces of text as Strings and the process of join bits of information together is sometimes called concatenation


Example 1

In this example, two pieces of text are joined together and displayed in a label. The message, “hello world”, will never change.

join1


Example 2

This example is a little more interesting. The text in the label will change depending on what the user
types into Text_Input1.

In many cases you’ll want to make sure that a user doesn’t leave an input blank, and we’ll cover how to do that in the next tip.


Example 3

You can join together as many strings as you like by clicking on the blue gear icon mutator (also know as a block mutator) and manually add in extra items, where ever you want them.

2 Likes

Is Empty

The is empty block can be used to check whether or not a string, or piece of text, contains and characters.

This block returns true if there are no characters in the string and returns false if there is one or more characters in a string.

isEmpty


Example 1

In this example we want to make sure that the user has typed something into Text_Input1. There are a couple of ways to do this.

These blocks do nothing if Text_Input1 is empty and only greet the user if they have typed in the text input.

These blocks also do nothing if Text_Input1 is empty and only greet the user if they have typed in the text input.

Finally, these blocks only do something if the text input is not empty. Note the dark green not block, which can be found in with the logic blocks.

Personally, this would be my preferred way of doing this, but it’s entirely up to you which option you go for.

2 Likes

Length Of

You can use the length of block in your projects to measure the length of a string.

It takes a stings as an input and returns the number of characters in that string as an output.

lengthOf


Example 1

This first example is an alternative way of approaching the previous tip. Here, we measure the length of the string to see if the user has typed in anything or not. If the length is greater than 1 (most names tend to be two letters or more!) then the greeting is displayed in the label.


Example 2

There are certain cases where you might want to make sure that a string has a certain minimum or maximum length.

For example, if you are asking your user to create a password, you might want to make sure that it is at least 6 characters long. The blocks below show how to do this.
min_length

In other cases, a password might have to be a minimum of 6 characters but a maximum of 32 characters in length.

You can check if it’s more then 5 and less than 33 character with the blocks below:

Bonus Tip!

Lots of blocks, such as the and block in the example above, take internal inputs by default so they look like this: int However, if the internal blocks get too long it can be difficult to read so you can change the shape of the block to accept external inputs instead, so it would look like this: ext NOTE we’re only changing the shape of the block here, the functionality of the block is completely unaffected.

In the previous example, if we right-click on the and block and choose “External Inputs” from the drop down menu…

…the and block will change shape, which might be a bit easier for you to read.

1 Like

Trim & Transform

The to UPPERCASE block is actually a text transform block that lets you choose whether you want to return an input string as “ALL UPPER CASE”, or as “all ower case” or as “All Title Case”, just click the little drop-down arrow to choose the option that suits your needs.

transform

Similarly, the trim spaces from both sides block also has a selector so that you choose to remove spaces from both sides, from the left side only or from the right side only.

trim


Example 1

In this scenario we are checking to see if a users password matches our password, which happens to be all lower case. Some keyboards can auto-correct or auto-capitalise text, and if your text input is just showing ***** to the user it can be very hard for them to spot this. Similarly, sometime a new space will be accidentally or automatically added and we want to trim these.

If you need to do this “trim and transform” operation in multiple places in your app, or if you want to tidy up multiple different text inputs for your user then you could create a dedicated function to do this:

So, example 1 above is somewhat simplified to look like this:

1 Like

New Line

As the name implies, the newline block allows you to put text onto a new line, i.e. it creates a line break in a string.

You can use as many newline blocks as you need to in your projects.

newline

Example 1

In this first example we just put a line break in between the two strings. This means that “hello” is on the first line and “world” is below that on the second line.

newline_example


Example 2

In this second example, you have a list of things and you want to display them all in a label. Rather than having all the items bunched together on a single line, you want each item to go on its own line.

3 Likes

New Line I give
IMG_20190724_173732

Haha.

2 Likes

BTW why deleted “\n” this thing and decided add a new block for it :thinking::thinking:
(And the release note hasn’t update yet :slight_smile:

1 Like

It depends on the text parser I think, so using a dedicated block will give a consistent UX across all currently-supported versions of iOS and Android.

2 Likes

Oh that’s mean former “\n” cannot support all of the device? I thought that it was a bug before :stuck_out_tongue:

1 Like

10 posts were split to a new topic: Question about the back pressed event

Hello, I need to know the length of a number, if it is composed of 1 or 2 numbers, how can I do it?

Length of block doesn’t work and gives unidentified result

There are ways to do this mathematically but you can also convert the number to text by joining it with “” (an empty string) and then check the length.

If you prefer to use math, you could divide by 10 and check if the remainder is <, > or = to 0 and that will tell you the number of digits.

2 Likes

nice :+1: :partying_face: :heart_decoration:

Can you please expand this??

What exactly are you trying to do?