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

FM for String Comparision

0 Likes
4,328

I have two variables like ABC and CDS and have text in those. Just I wanted to compare whether both are having same text or different text. If some characters are match then want to display those match characters...

Let me know any FM...

1 ACCEPTED SOLUTION
Read only

venkateswaran_k
Active Contributor
0 Likes
3,656

Hi Himanshu

You may check the following - example

split string2 at space into table data(wordlist).
then loop at wordlist
         match( val = <your_searchable_string> regex = <wordlist-word> occ = 1 )
     endloop

may need to correct for syntax

15 REPLIES 15
Read only

0 Likes
3,656

You can use CS operation

string compare

Read only

0 Likes
3,656

CO is not Contain Only ?

Read only

0 Likes
3,656

I Think CS we can only get contain string.. if exist then true.. but we did not get which string is matched....

Read only

venkateswaran_k
Active Contributor
0 Likes
3,656
Read only

0 Likes
3,656

I did not check my url link.

Read only

0 Likes
3,656

Sorry, I did not get you.

you are not able to open the link?

Read only

michael_piesche
Active Contributor
3,656

Checking whether they are same or different should be easy, right ?

But what are your requirements on checking for matches? You need to be more specific what kind of matches your are looking for and what the output should be? Because there can be more than 1 match.

ABC = 'ABKLBYZ'.
CDS = 'ABLKAYZ'.

I 'find' the following matches for above example:

  1. AB
  2. K
  3. L
  4. A
  5. B
  6. YZ

=> What 'matches' are you looking for? Are you just looking for matching prefixes of your variable, which should also be fairly easy, right?

Read only

venkateswaran_k
Active Contributor
0 Likes
3,657

Hi Himanshu

You may check the following - example

split string2 at space into table data(wordlist).
then loop at wordlist
         match( val = <your_searchable_string> regex = <wordlist-word> occ = 1 )
     endloop

may need to correct for syntax

Read only

Sandra_Rossi
Active Contributor
0 Likes
3,656

You mean a "diff" of strings? Eventually look at this other question.

Read only

0 Likes
3,656

Hi michael.piesche,

as per your example.. i am looking for only AB.....

Read only

0 Likes
3,656

Thank you for the short clarification on your requirements. Its still not a 100% clear on what the use-case is and what the input paramaters can be, but for now, see my answer above.

As a side note: Please use the comment function for comments to a specific thread, not the answer function to the question itself.

Read only

michael_piesche
Active Contributor
0 Likes
3,656
himanshukawatra.02

, so, you are only interested in a full match or a matching prefix, correct?In that case, the following coding would be simple and totally sufficient, but might have to tested and definitly be adapted in case your variables abc and cds are are either different length or strings to avoid shortdumps when one variables content is longer than the other. (eg use strlen( var ) to check the length of the content of your variable and only compare against partial prefixes of your variables abs and cds, when the prefix is shorter than the length of the content of your variable).

DATA match LIKE abc.  " matching prefix
DATA fullmatchfound   TYPE boole_d.
DATA prefixmatchfound TYPE boole_d.

IF abc = cds.
 match = abc.
 fullmatchfound = abap_true.
ELSE.
  sy-subrc = 0.
  WHILE sy-subrc = 0.
    IF abc(sy-index) = cds(sy-index).
      match = abc(sy-index).
      prefixmatchfound = abap_true.
    ELSE.
      sy-subrc = 8.
    ENDIF.
  ENDWHILE.
ENDIF.

Let me know if that helps. (I didnt test it, so let me know if it doesnt compile or whether it throws an unexpected dump)

Read only

0 Likes
3,656
DATA: lv_abc TYPE c LENGTH 10 VALUE 'ABCDEF',
      lv_def TYPE c LENGTH 10 VALUE 'ABCDEFG'.
  IF lv_abc CS lv_def AND strlen( lv_abc ) EQ strlen( lv_def ).
    WRITE : 'Same string'.
  ENDIF.
Read only

former_member618077
Discoverer
0 Likes
3,656

I created a code that show all the intersection.

types: begin of t_texto,
texto type char10,
end of t_texto.



DATA : x type char10 value 'ABCD',
y TYPE char10 value 'XBCYZ',
z type char10,
n type int2,
n1 type int2,
n2 type int2,
n3 type int2,

t1 type STANDARD TABLE OF t_texto,
r2 type RANGE OF t_texto,
sr like line of r2,
t3 TYPE STANDARD TABLE OF t_texto.

n = strlen( x ).

clear n1.

do n times.
n2 = 1.
while n2 <= n.
z = x+n1(n2).
APPEND z to t1.
add 1 to n2.
n3 = n1 + n2.
if n3 > n.
exit.
ENDIF.
ENDWHILE.
add 1 to n1.
ENDDO.

n = STRLEN( Y ).

clear n1.

DO n TIMES.
n2 = 1.
WHILE n2 <= n.
Z = y+n1(n2).

sr-sign = 'I'.
sr-low = z.
sr-option = 'EQ'.

APPEND sr TO r2.

ADD 1 TO n2.
n3 = n1 + n2.
IF n3 > n.
EXIT.
ENDIF.
ENDWHILE.
ADD 1 TO n1.
ENDDO.

delete t1 where texto not in r2.

LOOP at t1 into z.
write: / z.
ENDLOOP.
Read only

michael_piesche
Active Contributor
0 Likes
3,656

himanshukawatra.02, please follow up on your open question.

  • comment answers or your question if there are still open issues.
  • otherwise mark an answer as accepted if it helped you solve your problem
  • or post an answer of yourself and accept it if you found another useful solution yourself
  • or redirect your question to another question that is related and was useful to solve your problem
  • in the end, close your question