Application Development and Automation 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: 
Read only

Counting SPACES in a string field.

Former Member
0 Likes
2,658

Hi ,

can any one plz send me the code for counting the number of spaces in a particular string.

like if we have the value as 100 00 0000. we must get the out put as 2 .

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,288

data : a type string,

b type string,

c type i,

d type i,

e type i.

a = '100 100 100'.

move a to b.

condense b no-gaps.

c = strlen( a ).

d = strlen( b ).

e = c - d.

write 😕 'number of gaps' , e.

Hi ,

can any one plz send me the code for counting the number of spaces in a particular string.

like if we have the value as 100 00 0000. we must get the out put as 2 .

3 REPLIES 3
Read only

Former Member
0 Likes
1,288

try this...

DATA : str TYPE string VALUE '100 00 0000',

str_len TYPE i,

str_len2 TYPE i,

space_count TYPE i.

str_len = STRLEN( str ).

CONDENSE str NO-GAPS.

str_len2 = STRLEN( str ).

space_count = ( str_len - str_len2 ).

WRITE 😕 space_count.

Read only

Former Member
0 Likes
1,289

data : a type string,

b type string,

c type i,

d type i,

e type i.

a = '100 100 100'.

move a to b.

condense b no-gaps.

c = strlen( a ).

d = strlen( b ).

e = c - d.

write 😕 'number of gaps' , e.

Read only

JozsefSzikszai
Active Contributor
0 Likes
1,288

hi Sameer,

1. count the length of the string (with STRLEN...)

2. CONDENSE string NO-GAPS (this will remove all spaces)

3. count the length of the string (with STRLEN...) - again

4. calculate the difference between the first and second count, that will be the number of spaces in your string.

hope this helps

ec