Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Finding trailing spaces in a string variable

former_member215563
Active Participant
0 Kudos

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

1 ACCEPTED SOLUTION

pokrakam
Active Contributor

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
3 REPLIES 3

pokrakam
Active Contributor

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

0 Kudos

This worked for me 🙂 Thanks a ton Mike !!!