Skip to Content
author's profile photo Former Member
Former Member

assign x type data to xstring type issue

hi

how assign x type to xstring.

and get actual length.

example:

data: len type i,

x1(300) type x,

xs type xstring.

case 1:

xs = '2222'.

len = xstrlen( xs ).

we will get len value is 2 which result that i want to get.

case 2.

x1 = '2222'.

len = xstrlen( x1 ).

we will get len value is 300.

xs = x1.

len = xstrlen( xs ).

we will get len value is 300.

i know in var x1 will fill with '00' from left to right. but i want to know actual length not definition lenth.

thank you very much advance.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

5 Answers

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 03:35 AM

    hi,

    Use describe statement i.e,

    data : len type i. 
    
    DESCRIBE FIELD x1 length len.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 03:44 AM

    xstrlen will return the actual lenghth only in case of hexstrings.

    For x type variables with fixed length xstrlen will return the defined lenghth always.

    Try using strlen instead of xstrlen for such cases to get correct occupied lenght. In 4.6 it works. I do not know what release you are on.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 04:17 AM

    thank you!

    release is 6.4!

    replace string type to xstring type and get length is definition length yet.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 04:51 AM

    Hi xiaobing xue,

    There is no Function Module in SAP that actually removes the Zero's after the numbers ('222200000000'). CONVERSION_EXIT FM's are there to remove Leading Zero's and add Leading Zero's. So I think you need to manually proceed and calculate the length. Try the following code and you'll get the desired output.

    DATA: len TYPE i,

    x1(300) TYPE x,

    xs TYPE xstring,

    xs1 TYPE xstring.

    DATA: ch1 TYPE char255,

    ch2 TYPE char255.

    DATA: l1 TYPE i,

    l2 TYPE i.

    xs = '2222'.

    len = XSTRLEN( xs ).

    WRITE:/ 'First Length Calculation', len.

    CLEAR: len.

    x1 = '2222'.

    len = XSTRLEN( x1 ).

    DO len TIMES.

    IF x1+l2(1) IS NOT INITIAL.

    MOVE x1+l2(1) TO ch2.

    CONCATENATE ch1 ch2 INTO ch1.

    l2 = l2 + 1.

    ELSE.

    EXIT.

    ENDIF.

    ENDDO.

    MOVE ch1 TO xs1.

    len = XSTRLEN( xs1 ).

    WRITE:/ 'Second Length Calculation', len.

    CLEAR: len, ch1, ch2, l2.

    xs = x1.

    len = XSTRLEN( xs ).

    DO len TIMES.

    IF xs+l2(1) NE '00'.

    MOVE xs+l2(1) TO ch2.

    CONCATENATE ch1 ch2 INTO ch1.

    l2 = l2 + 1.

    ELSE.

    EXIT.

    ENDIF.

    ENDDO.

    MOVE ch1 TO xs1.

    len = XSTRLEN( xs1 ).

    WRITE:/ 'Third Length Calculation', len.

    Hope that this solves your problem. Reward accordingly.

    Thanks and Regards,

    Maddineni Bharath.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Apr 09, 2008 at 04:58 AM

    thank you

    this

    CLEAR: len, ch1, ch2, l2.

    xs = x1.

    len = XSTRLEN( xs ).

    DO len TIMES.

    IF xs+l2(1) NE '00'.

    MOVE xs+l2(1) TO ch2.

    CONCATENATE ch1 ch2 INTO ch1.

    l2 = l2 + 1.

    ELSE.

    EXIT.

    ENDIF.

    ENDDO.

    MOVE ch1 TO xs1.

    len = XSTRLEN( xs1 ).

    WRITE:/ 'Third Length Calculation', len.

    is i want!

    thank you !

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.