0 Favourites

How do I access characters in a string

  • 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?

  • 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:

    [quote:3s0xjxgi]

    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'.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Thank you very much! That was exactly what I was searching for <3

  • 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?

  • Thanks!!! I was trying to find the mid()

  • 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:

    myString = "Avengium";
    myCharacters = right(myString, 3)
    [/code:28xs41ez]
    
    To get the 3rd and 4th to the last, "ig" in the example below, you must do the following:
    [code:28xs41ez]
    myString = "Avengium";
    myCharacters = mid(myString, len(myString) - 3, 1) & mid(myString, len(myString) - 4, 1)
    [/code:28xs41ez]
Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)