5

I have the labels, which include a few of the same characters "/". Unfortunately, I cannot simply use the "Wrap on character" option, because it wraps everywhere where this character occurs.

enter image description here

I am wondering about the option, which would allow me to wrap the text at some nth character instead of each.

2
  • I want to have it wrapped after 4th slash
    – Geographos
    Commented Mar 17, 2022 at 17:34
  • 3
    You can find some way of inserting some unused char every fourth slash and use that char to wrap by
    – Bera
    Commented Mar 17, 2022 at 17:56

2 Answers 2

4

Using QGIS expressions and @BERA suggestion:

in the label field write the expression:

regexp_replace ("FieldName", '(.+)(/)(.+)$','\\1:\\3')

then, use : to return the last part of the text

enter image description here

5
  • 1
    this expression will replace the last occurrence of /, not necessarily the 4th and not every 4.
    – JGH
    Commented Mar 17, 2022 at 19:14
  • if you can, publish a small dataset
    – pigreco
    Commented Mar 17, 2022 at 19:25
  • @JGH the 4th is just an example. At least I should know how to set the breakdown in any nth occurrence other than each.
    – Geographos
    Commented Mar 17, 2022 at 19:26
  • 1
    @MKR to do it in a timely manner and based on your example: regexp_replace( 'VON/FLN/CBF-DUN2/A1/FDN199', '(.+)(/)(.+)(/)(.+)(/)(.+)(/)(.+)','\\1\\2\\3\\4\\5\\6\\7:\\9'): to replace the 4th /
    – pigreco
    Commented Mar 17, 2022 at 19:45
  • or using arrays: array_to_string(array_slice( string_to_array('VON/FLN/CBF-DUN2/A1/FDN199','/'),0,3),'/') || '\n' || array_to_string( array_slice( string_to_array('VON/FLN/CBF-DUN2/A1/FDN199','/'),4,-1),'/')
    – pigreco
    Commented Mar 17, 2022 at 20:37
2

Another solution using Arrays.

If you want to do it every x times, you can use arrays like this:

with_variable('pos',"pos",
array_to_string(array_slice( string_to_array("text",'/'),0,eval(@pos)),'/')  
||'\n'|| -- new line
array_to_string( array_slice( string_to_array("text",'/'),eval(@pos)+1,-1),'/'))

the "pos" attribute has a range of 0-3, because arrays start at zero.

enter image description here

enter image description here

enter image description here

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.