4 Replies Latest reply: May 14, 2012 9:57 AM by Kesavadas Thekkillath RSS

How to search a value in a filed type xstring

DAVIDE GROSSO
Currently Being Moderated

Dear all, I need to search a special characters "# - that  is equal to return to next line" in a string field and to replace it.

 

Becuase this is  a special character, is not possible to search it directly in the string filed but i need to use a XSTRING definition.

I'm using this form, that works correctly, but it's slow, because for each line it needs to scan all the character of the string to search the "#"(

<critical_x> = '000D' ) that I'm interested.

 

 

  conta = 0.
        DO .

* Scansione della nota per eliminare gli eventuali cancelletti
          ASSIGN wa_lines-tdline+conta(1) TO <critical_x> CASTING.
          IF <critical_x> IS NOT ASSIGNED.
            EXIT.
          ENDIF.
          IF <critical_x> = '000D'.
            <critical_x> = '0020'.
          ENDIF.
          IF <critical_x> = '000A'.
            <critical_x> = '0020'.
          ENDIF.
          conta = conta + 1.

        ENDDO.

 

My question is, there is another way to do it ?

Are there faster possibilities ?

I check that the ABAP command SEARCH doesn't work on binary field.

Thank you in advance

 

Davide

  • Re: How to search a value in a filed type xstring
    Bhanu Malik
    Currently Being Moderated

    Hi Davide,

    You can use  "FIND FIRST OCCURRENCES OF REGEX in your string.

     

    For example i am searching for space in my sentence:

       FIND ALL OCCURRENCES OF REGEX '\S+'  IN I_SEARCH_STRING RESULTS LT_WORDS.

     

    You can use find command to search for special charcters

     

    Or you can also use CA (Contains Any) , CO (Contain only) keywords to search for special character in a string

  • Re: How to search a value in a filed type xstring
    Akshay Gujrati
    Currently Being Moderated

    Dear David,

     

    You can use following options to find out your special characters.

     

    1. REPLACE ALL OCCURRENCE OF '#' IN "UR_STRING" BY "SAPCE".

    USE CONDENSE TO REMOVE SPACES.

     

    2.You can go for CP(Contains Pattern), CO(Contains Only) etc as described follows:

     

    CO (Contains Only)

    The logical expression

    <f1> CO <f2>

    is true if <f1> contains only characters from <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that does not occur in <f2> .

    CN (Contains Not only)

    The logical expression

    <f1> CN <f2>

    is true if <f1> does also contains characters other than those in <f2>. The comparison is case-sensitive. Trailing blanks are included. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that does not also occur in <f2>. If it is false, SY-FDPOS contains the length of <f1>.

    CA (Contains Any)

    The logical expression

    <f1> CA <f2>

    is true if <f1> contains at least one character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of the first character of <f1> that also occurs in <f2> . If it is false, SY-FDPOS contains the length of <f1>.

    NA (contains Not Any)

    The logical expression

    <f1> NA <f2>

    is true if <f1> does not contain any character from <f2>. The comparison is case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of the first character of <f1> that occurs in <f2> .

    CS (Contains String)

    The logical expression

    <f1> CS <f2>

    is true if <f1> contains the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.

    NS (contains No String)

    The logical expression

    <f1> NS <f2>

    is true if <f1> does not contain the string <f2>. Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .

    CP (Contains Pattern)

    The logical expression

    <f1> CP <f2>

    is true if <f1> contains the pattern <f2>. If <f2> is of type C, you can use the following wildcards in <f2>:

     

    • for any character string *
    • for any single character +

    Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the offset of <f2> in <f1> . If it is false, SY-FDPOS contains the length of <f1>.If you want to perform a comparison on a particular character in <f2>, place the escape character # in front of it. You can use the escape character # to specify

     

    • characters in upper and lower case
    • the wildcard character "*" (enter #* )
    • the wildcard character "+" (enter #+ )
    • the escape symbol itself (enter ## )
    • blanks at the end of a string (enter #___ )

    NP (contains No Pattern)

    The logical expression

    <f1> NP <f2>

    is true if <f1> does not contain the pattern <f2>. In <f2>, you can use the same wildcards and escape character as for the operator CP.

    Trailing spaces are ignored and the comparison is not case-sensitive. If the comparison is true, the system field SY-FDPOS contains the length of <f1>. If it is false, SY-FDPOS contains the offset of <f2> in <f1> .


    DATA: F1(5) TYPE C VALUE <f1>,

          F2(5) TYPE C VALUE <f2>.

    IF F1 <operator> F2.
       WRITE: / 'Comparison true, SY-FDPOS=', SY-FDPOS.
    ELSE.
       WRITE: / 'Comparison false, SY-FDPOS=', SY-FDPOS.
    ENDIF.

    The following table shows the results of executing this program, depending on which operators and values of F1 and F2.

     

    <f1>

    <operator>

    <f2>

    Result

    'BD '

    CO

    'ABCD '

    true

    'BD '

    CO

    'ABCDE'

    false

    'ABC12'

    CN

    'ABCD '

    true

    'ABABC'

    CN

    'ABCD '

    false

    'ABcde'

    CA

    'Bd '

    true

    'ABcde'

    CA

    'bD '

    false

    'ABAB '

    NA

    'AB '

    false

    'ababa'

    NA

    'AB '

    true

    'ABcde'

    CS

    'bC '

    true

    'ABcde'

    CS

    'ce '

    false

    'ABcde'

    NS

    'bC '

    false

    'ABcde'

    NS

    'ce '

    true

    'ABcde'

    CP

    '*b*'

    true

    'ABcde'

    CP

    '*#b*'

    false

    'ABcde'

    NP

    '*b*'

    false

    'ABcde'

    NP

    '*#b*'

    true

     

     

    With Regards,

    Akshay

Actions