Skip to Content
-2

Finding trailing spaces in a string variable

Aug 04, 2017 at 04:04 PM

86

avatar image
Former Member

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?

Thanks,

Faiz

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Mike Pokraka Aug 04, 2017 at 04:40 PM
2

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
Show 1 Share
10 |10000 characters needed characters left characters exceeded
Former Member

This worked for me :) Thanks a ton Mike !!!

0
Horst Keller
Aug 05, 2017 at 07:32 AM
1
Share
10 |10000 characters needed characters left characters exceeded