How do I access characters in a string

Get help using Construct 2

Post » Wed Sep 02, 2015 10:57 am

Hi,

I have a String like this "Hello World"
How do I get specific characters of this string like the second letter? Is this even possible?
B
4
Posts: 4
Reputation: 208

Post » Wed Sep 02, 2015 11:07 am

Mijdax wrote:Hi,

I have a String like this "Hello World"
How do I get specific characters of this string like the second letter? Is this even possible?


There are numerous System expressions that deal with text in the Manual.

Some that may be of use to you are:


find(src, text)
Find the first index within src that text occurs, else returns -1.

left(text, count)
Return the first count characters of text.

len(text)
Return the number of characters in text.

mid(text, index, count)
Return the count characters starting from index in text.

right(text, count)
Return the last count characters of text.


So for instance, if I had a string such as "My World" stored in a variable called 'mytxt', then

mid(mytext, 4,1) would just show the letter 'o'.
If your vision so exceeds your ability, then look to something closer.
Moderator
B
136
S
31
G
86
Posts: 5,482
Reputation: 59,750

Post » Wed Sep 02, 2015 9:46 pm

Thank you very much! That was exactly what I was searching for <3
B
4
Posts: 4
Reputation: 208

Post » Fri May 26, 2017 12:01 pm

Hi. Thanks for the info. If i want to extract the last 3 characters of a string at once, how i do this?
Making right(text, 3) right(text, 2) right(text, 1), making right(text, 3)? or other thing?
If i want to extract (counting from right to left) character 3 and 4 at once?
B
9
Posts: 14
Reputation: 353

Post » Fri Oct 06, 2017 4:18 pm

Thanks!!! I was trying to find the mid()
B
7
S
1
Posts: 3
Reputation: 447

Post » Fri Oct 06, 2017 6:38 pm

Avengium wrote:Hi. Thanks for the info. If i want to extract the last 3 characters of a string at once, how i do this?
Making right(text, 3) right(text, 2) right(text, 1), making right(text, 3)? or other thing?
If i want to extract (counting from right to left) character 3 and 4 at once?


Note that right(text, n) gets n characters starting at the last character of the string and going backwards, like left(text, n) gets n characters starting at the begining of the string and going forward.

If you need to get a certain amount of characters in the middle of a string you should use mid(text, start, n), which will return n characters starting at start and going forward.

Thus, if you need to return the 3rd to last followed by the 4th to last you would need to use mid(0 twice and join the characters toghether:

Examples
To get the last 3 characters, "ium" in the example below, you must do the following:
Code: Select all
myString = "Avengium";
myCharacters = right(myString, 3)


To get the 3rd and 4th to the last, "ig" in the example below, you must do the following:
Code: Select all
myString = "Avengium";
myCharacters = mid(myString, len(myString) - 3, 1) & mid(myString, len(myString) - 4, 1)
Bruno

I like to eat, but I'm no cook. Why liking to play games would make you a game designer?
B
20
S
6
G
2
Posts: 620
Reputation: 3,492


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 8 guests