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: 

What is the difference between strlen and numofchar...??

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi All,

I am pretty novice in the ABAP Development. Please let me know the difference between using strlen and numofchar. I used both of them with string and character as well. It returned the same results. This is the code snippet of the code.

and this is the output when the input parameter is "sumit kumar"


1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Sumit,

Here you go. F1 could have saved the trouble of posting here.

numofcharSame as for the function strlen, except that in this case when a non-Unicode double-byte code is used, a character that occupies 2 bytes is only counted once.
strlenNumber of characters in arg; blanks at the end of data objects with a fixed length are not counted, although they are counted in type string data objects. If you use a non-Unicode double-byte code, a character that occupies 2 bytes is counted twice.

Regards,

Shravan

13 REPLIES 13

Former Member
0 Kudos

Hi Sumit,

Here you go. F1 could have saved the trouble of posting here.

numofcharSame as for the function strlen, except that in this case when a non-Unicode double-byte code is used, a character that occupies 2 bytes is only counted once.
strlenNumber of characters in arg; blanks at the end of data objects with a fixed length are not counted, although they are counted in type string data objects. If you use a non-Unicode double-byte code, a character that occupies 2 bytes is counted twice.

Regards,

Shravan

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Shravan,

Could you please site a small example to differentiate between the two...Thanks

Sumit Kumar

0 Kudos

Hi Sumit,

You will see the difference when you are using a double byte characters (Eg. japanese/chinese).

For a text with 4 such alphabets  numberofchar will be 4 but  strlen will be 8. 

Regards,

Shravan

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Shravan,

Thanks a lot for your contribution. Cheers...!!

Regards

Sumit Kumar

0 Kudos

Hi Shravan,

I don't think so.

Clemens

Clemenss
Active Contributor
0 Kudos

Hi Sumit,

{Christmas wish list: F1 key}

numofchar:


Number of characters in arg, where closing blanks are not counted in data objects with fixed lengths or in data objects with the type string. If you use a non-Unicode double-byte code, a character that uses two bytes is only counted once.

strlen:

Number of characters in arg, where closing blanks in data objects with fixed lengths are not counted. They are counted though in data objects with the type string. If you use a non-Unicode double-byte code, a character that uses two bytes is counted twice. The argument can be byte-like outside Unicode programs.

I think tha difference to be noted is that closing blanks in strings are counted with strlen, not with numofchar.

lv_string = '1234  '.

strlen( lv_string ) => 6,

numofchar( lv_string ) => 4,

Regards

Clemens

Question: What is a non-Unicode double-byte code? I don't know.

Regards,

Clemens. 

Former Member
0 Kudos

Hi Clemens,

A character in language like Japanese is an example of non-Unicode double-byte code. These characters need to be represented in 2 bytes because 1 byte (256 characters) is not enough for the alphabets in such languages (They have over thousands alphabets/symbols). 

Also I wrote a simple program to test what you were saying. I got an output of 4 and 4. I don't think the difference is with spaces.

data: lv_value(10),

       lv_char TYPE string.

lv_char = '1234   '.

lv_value = strlen( lv_char ) .

WRITE lv_value.

lv_value = numofchar( lv_char ) .

WRITE lv_value.

Regards,

Shravan

Former Member
0 Kudos

Now when I come to think of it. In a unicode system where all literals take up 2 bytes there might not be any difference at all but in a non-unicode I still think the results will be different for languages like Japanese. I do not have any such languages installed to check on my system. Can someone validate/enlighten? 

Regards,

Shravan

Clemenss
Active Contributor
0 Kudos

Hi Shravan,

sorry, I did not test. Now I did and I can confirm your sample. I tried a bit different using back-quote-delimited string that preserves spaces:

Here we have the difference:

As I already mentioned: I never saw a double-byte-non-unicode system. In today's unicode systems, just all characters are double-byte, regardless if chinese, english or what else.

Regards

Clemens

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Clemens,

Thanks for the information.

Regards

Sumit

Former Member
0 Kudos

Hi Clemens,

Thanks for the code. I did not know of the quotes `` which preserve spaces. I have struggled in the past with that. My learning for the day.

Sumit,

As far as the double byte chars go the difference can only be observed in a non-unicode system or at least I would like to believe so till I get my hands on a system where I can validate this. 

Regards,

Shravan

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Clemens & Shravan,

Sincere thanks for the feeds...actually I too tried to discover it for myself...got to know it basically implies if there are any spaces which are bounded by any characters or not...if any space is not bounded by characters it counts in numofchar where as in case of strlen it does not...

Sumit

SUMIT1
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Clemens & Shravan,

Sincere thanks for the feeds...actually I too tried to discover it for myself...got to know it basically implies if there are any spaces which are bounded by any characters or not...if any space is not bounded by characters it counts in numofchar where as in case of strlen it does not...

Sumit