Skip to Content

Finding trailing spaces in a string variable

Hi All,

I have to build a JSON string by concatenating couple of string variables. Lets say str1 = 'The Sun is shining ' str2 = 'and the weather is nice'. Now the issue is:-

1. Concatenate str1 str2 will result in -The Sun is shiningand the weather is nice.

i.e it will remove the trailing space from str1.

2. Respecting blanks will not work since its a string variable and I cannot use a char as per my req where the length of the variable varies.

Therefore I thought of finding the number of trailing white space in str1 i.e. FIND ' ' in str1 results tab1 and then adding those spaces in the final string. But that also doesn't seem to work.

Is there any other way I can solve this?



Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Aug 04, 2017 at 04:40 PM

    CONCATENATE is a fine example of the difference between strings and chars. By your description, it sounds like you are working with fields of type C, which is not the same as a string. Search string vs char, there have been a few articles written on SCN.

    output->write( 'Hello' && ' ' && 'world' ).  "text is type C, space = initial, will be removed.
    output->write( `Hello` && ` ` && `world` ).  "Text is type string, ` ` is not initial, because ` `<>``. Space stays. 

    My personal favourite is to use string templates. If you've never used them, they take a bit of getting used to, but reading the online help is definitively worthwhile:

    output->write( |{ `Hello` } { `world` }| ).   "will preserve the space regardless of the data types
    Add comment
    10|10000 characters needed characters exceeded

  • Aug 05, 2017 at 07:32 AM
    Add comment
    10|10000 characters needed characters exceeded