3

I want to use QGIS expressions to split up a text string to an array with each element representing one character of the input's string, like:

input: mytext -> ouput: [ 'm', 'y', 't', 'e', 'x', 't' ]

I tried regular expressions with regexp_matches() function, but was not able to achieve it but partially:

regexp_matches( ('mytext'),'(.+)(.)(.+)') -> [ 'myte', 'x', 't' ]

I could, of course, use the same numer of capturing group (.) as the input has characters. But the goal is an expression that works with any number of characters. How to achieve that?

1 Answer 1

7

Don't think so complicated ;)

array_remove_all(string_to_array('mytext',''),'')

Result: [ 'm', 'y', 't', 'e', 'x', 't' ].

For some reason string_to_array() adds empty stuff ('') to the array as first and last position, so we remove it with array_remove_all()

1
  • I believe the logic behind string_to_array() returning empy values for the first and last elements of the array, when splitting a string into individual characters using an empty string delimter, is that there is an empty string before the first character and an empty string after the last character just like there is an empty string between each character. I consider QGIS string_to_array() to be more like a text "split" method/function rather than a text "to character array" method/function.
    – bixb0012
    Commented Nov 24, 2023 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.