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

comparing strings

Former Member
0 Likes
1,071

Hi experts how to find common word in two strings.

say suppose

data : name1(30) value 'sales order documnet',

name2(50) value 'purchase order document'.

here in name1 and name2 having tow words common say order and document.

here say matching word count for order is 1.

here say matchin words count for document is 1.

like that i have to compare 2 strings and matching words i have to count.

how to it.

Thanks

1 ACCEPTED SOLUTION
Read only

viquar_iqbal
Active Contributor
0 Likes
1,040

Hi

If you know the word which is common then use Contains String (CS).

or use FIND press F1 on find.

Thanks

Viquar Iqbal

7 REPLIES 7
Read only

viquar_iqbal
Active Contributor
0 Likes
1,041

Hi

If you know the word which is common then use Contains String (CS).

or use FIND press F1 on find.

Thanks

Viquar Iqbal

Read only

0 Likes
1,040

That is fine but i have to check in runtime.i dont know exact word.

that is my requirement

Thanks

Read only

0 Likes
1,040

This code might help u but also check with the options of search,find statements


data:text1 type string value 'sales order document'.
data:text2 type string value 'purchase order document'.
data:mcount type i.

data:begin of it_text occurs 0,
        value type char255,
 end of it_text.




split text1 at space into table it_text.

loop at it_text.
  if text2 cs  it_text-value.
    mcount = mcount + 1.
  endif.
endloop.

write mcount.

Read only

0 Likes
1,040

Hi keshu,

Good try.

but here i am getting count as 0.

even i have count for order is 1 in text2 and count for document is 1.

my requirement is i have to count individuvally how many words are matching text1 in text2.

their individual count i have to print.

Thanks

Read only

0 Likes
1,040

check this


data:text1 type string value 'sales order document'.
data:text2 type string value 'purchase order document'.
data:mcount type i.

data:begin of it_text occurs 0,
        value type char255,
 end of it_text.


data:begin of it_text2 occurs 0,
        value type char255,
 end of it_text2.




split text1 at space into table it_text.

split text2 at space into table it_text2.

loop at it_text.
  mcount = 0.
  loop at it_text2 where value = it_text-value.
    mcount = mcount + 1.
  endloop.
  skip 1.
  if mcount > 0.
    write: it_text-value,mcount.
   endif.
endloop.

Read only

0 Likes
1,040

Hi keshu,

you done great job for me.

i given good points to u.

Thanks

Read only

former_member585060
Active Contributor
0 Likes
1,040

Hi,

Try below code

TYPES : BEGIN OF ty_text,
          text(50) TYPE c,
         END OF ty_text.

DATA : str1 TYPE string,
       str2 TYPE string.

DATA : cnt TYPE i.

DATA : it_itab TYPE TABLE OF ty_text,
        wa_itab TYPE ty_text,
        it_itab1 TYPE TABLE OF ty_text,
         wa_itab1 TYPE ty_text.

str1 = 'sales order document hello go'.
str2 = 'purchase order document hello go'.

SPLIT str1 AT space INTO TABLE it_itab.
SPLIT str2 AT space INTO TABLE it_itab1.

LOOP AT it_itab INTO wa_itab.
  READ TABLE it_itab1 INTO wa_itab1 WITH KEY text = wa_itab-text.
  IF sy-subrc = 0.
    cnt = cnt + 1.
  ENDIF.
ENDLOOP.

cnt will have the matched no of word count

Regards

Bala Krishna