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

Regarding finding the mismatching characters in two strings.

Former Member
0 Likes
1,521

Hi all,

Please tell me how to find the differing characters between two strings.

For ex str1 = 'asdwe'.

str2 = 'asd'.

Then i should get 'we' as result.

Thanks.

1 ACCEPTED SOLUTION
Read only

Sm1tje
Active Contributor
0 Likes
1,487

Probably the safest way would be to process both string character by character, something like this:


DO.
offset = offset + 1.
lv_char1 = str1+offset(1).
lv_char2 = str2+offset(1).

IF lv_char1 <> lv_char2.
***do whatever you want***
ENDIF.
ENDDO.

Probably the safest way would be to process both string character by character, something like this:


DO.
offset = offset + 1.
lv_char1 = str1+offset(1).
lv_char2 = str2+offset(1).

IF lv_char1 <> lv_char2.
***do whatever you want***
ENDIF.
ENDDO.

12 REPLIES 12
Read only

Sm1tje
Active Contributor
0 Likes
1,488

Probably the safest way would be to process both string character by character, something like this:


DO.
offset = offset + 1.
lv_char1 = str1+offset(1).
lv_char2 = str2+offset(1).

IF lv_char1 <> lv_char2.
***do whatever you want***
ENDIF.
ENDDO.

Read only

GauthamV
Active Contributor
0 Likes
1,487

Use comparison operators.

[http://help.sap.com/saphelp_erp2004/helpdata/EN/fc/eb3516358411d1829f0000e829fbfe/frameset.htm]

Read only

Former Member
0 Likes
1,487

Hi,

use the FIND statement to find the string usbset. once found, find the offset at which the subset occurs. The startingt ta offset u can read the remaining characters.

Read only

Former Member
0 Likes
1,487

Hi,

Use the following code,



data: str1(70) VALUE 'asdwedkre[qriomaaaaaawfhupfewpfjsnjiheuwiqfjaaaaa',
      str2(10) VALUE 'a'.


replace ALL OCCURRENCES OF str2 in str1 WITH ''.   

the output is sdwedkre[qriomwfhupfewpfjsnjiheuwiqfj without any blacks or spaces.

Edited by: Rajat Chaturvedi on Mar 16, 2009 11:54 AM

Read only

Former Member
0 Likes
1,487

Hi,

This is very simple, hope the below snippet help you out:

data:String1(5) type c value 'asdwe',
     String2(3) type c value 'asd',
     String3(2) type c.

if String1 CN String2.
String3 = String1+3(2).
endif.

write:/ String3.

PS:if the lenght of the strings are not known then find out the lenght of the 2 strings and use the offset from the fixed value to the length of the strings calculated.

Moreover I am assuming that if String1 contains String2 in the starting position only.

Pooja

Read only

Former Member
0 Likes
1,487

try this:

calculate the length of the 1st string: v_len1

calculate the length of the 2nd string: v_len2

now,

v_len1 = STRLEN( v_text1)

v_len2 = STRLEN( v_text2)

v_next1 = 0.

v_count = 0.

v_next2 = 0.

v_count = 0.

    • comparing each letter in 1st string with each letter in 2nd string.

DO v_len1 TIMES.

DO v_len2 TIMES.

if v_textv_next1(v_len1) <> v_text2v_next2(v_len2). v_text3 = v_text+v_next(v_len1).

ENDIF.

v_next2 = v_next2+1.

ENDDO.

v_next1 = v_next1+1.

ENDDO.

v_text3 will hve the difference.

Read only

Former Member
0 Likes
1,487

hi,

do like this,

var1 = asd.

var2 = asdewasdew..

then take strlen(var1) into count.

then count holds value 3.

i =1.

do count times..

var3 = var1+0(i).

i = i + 1.

replace all occurences of var3 in var2 by space..

codense var2.

enddo.

  • Here var3 holds value 'a' , in var2 a is been replaced by space and var2 holds sdewsdew..

same way repeat for other two characters also then u r left with ewew..

i think this is ur requireed output..

Rgds.,

subash

Read only

0 Likes
1,487

sorry small change in logic

var1 = asd.

var2 = asdewasdew..

then take strlen(var1) into count.

then count holds value 3.

i =1.

a= 0.

do count times..

var3 = var1+a(i)

i = i + 1.

a= a + 1.

replace all occurences of var3 in var2 by space..

codense var2.

enddo.

Read only

Former Member
0 Likes
1,487

check this code.


DATA: st1 TYPE char3 VALUE 'asd', "Important: type length should be equal to value
      st2 TYPE char10 VALUE 'asdwe'.


WRITE: / st2.   "output: asdwe
CONDENSE st1.
REPLACE st1 WITH space INTO st2.
CONDENSE st2.
WRITE: / st2.   "output: we

кu03B1ятu03B9к

Read only

former_member222860
Active Contributor
0 Likes
1,487

here's the logic:

DATA: counter TYPE i value '5',
      flag,
      str1 TYPE char12 VALUE 'asdwew',
      str2 TYPE char12 VALUE 'asd',
      str3 TYPE char12,
      len type i.

DO counter TIMES.
  len = strlen( str1 ).
  IF str1(sy-index) = str2(sy-index).
    flag = 'X'.
   ELSE.
    sy-index = sy-index - 1.
    move str1+sy-index(len) to str3.
    write:/ str3.
    EXIT.
  ENDIF.
ENDDO.

thanks\

Mahesh

Read only

Former Member
0 Likes
1,487

Hi,

Try this.

DATA : lv_lenstr1 TYPE i,

lv_lenstr2 TYPE i,

idx_i TYPE i,

idx_j TYPE i,

lv_str3 TYPE string,

lv_char1 TYPE c,

lv_char2 TYPE c.

PARAMETERS : p_str1 TYPE string,

p_str2 TYPE string.

lv_lenstr1 = STRLEN( p_str1 ).

lv_lenstr2 = STRLEN( p_str2 ).

IF ( lv_lenstr1 GT lv_lenstr2 ). "if first string is greater than second

"if second string is greater interchange

"first and second string

CLEAR : idx_i,

idx_j.

DO lv_lenstr1 TIMES.

lv_char1 = p_str1+idx_i(1).

CLEAR : idx_j.

DO lv_lenstr2 TIMES.

IF idx_i LE lv_lenstr2.

lv_char2 = p_str2+idx_j(1).

IF lv_char1 EQ lv_char2.

EXIT.

ELSE.

IF p_str2 CA lv_char1.

EXIT.

ELSE.

CONCATENATE lv_str3 lv_char1 INTO lv_str3.

EXIT.

ENDIF.

ENDIF.

idx_j = idx_j + 1.

ENDIF.

ENDDO.

IF idx_i GT lv_lenstr2.

IF p_str2 CA lv_char1.

idx_i = idx_i + 1.

CONTINUE.

ELSE.

CONCATENATE lv_str3 lv_char1 INTO lv_str3.

ENDIF.

idx_i = idx_i + 1.

ELSE.

idx_i = idx_i + 1.

ENDIF.

ENDDO.

ENDIF.

WRITE lv_str3.

Regards,

Roopa

Read only

Former Member
0 Likes
1,487

declare two internal tables.

data:begin of lt_string1 occurs 0,

line(1) type c,

end of lt_string1.

data: begin of lt_string2 occurs 0,

line(1) type c,

end of lt_string 2.

data: lv_char(10) type c value 'abcd'

data: lv_ch(10) type c value 'ab'.

lt_string1-line = lv_char+0(1)

append lt_string1.

lt_string1-line = lv_char+1(1)

append lt_string1.

and so on....

do the same thing with another internal table .

now the two tables have structures like this

lt_string1 lt_string2

-


-


a a

b b

c

d

now

loop at lt_string1.

read table lt_string2 with key line = lt_string1-line.

if sy-subrc <> 0.

lt_string3-line = lt_string1-line

append lt_string3.

endif.

lt_string3 will have c and d

reg