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

Work with strings

Former Member
0 Likes
1,646

Hi!

How to find common characters in two strings, when the user enters strings himself?

8 REPLIES 8
Read only

TammyPowlas
SAP Mentor
SAP Mentor
1,435

Linda - I recommend searching

I searched a found some suggestions here: https://answers.sap.com/questions/3228489/hw-to-compare-two-strings.html

Along with a wiki: https://wiki.scn.sap.com/wiki/display/ABAP/String+Processing

Read only

Sandra_Rossi
Active Contributor
Read only

former_member1716
Active Contributor
1,435

Linda Yan,

Below is the code for you. I Have defined two Parameters, You can enter any Value in the two Parameters and it will write the common Characters for you.

PARAMETERS: p_str1 TYPE string DEFAULT 'TEXT1234',
            p_str2 TYPE string DEFAULT 'TEXT'.

DATA: lv_tmp TYPE string.

lv_tmp = p_str1.
CONCATENATE '[' p_str2 ']' INTO p_str2.
REPLACE ALL OCCURRENCES OF REGEX p_str2 IN p_str1 WITH `` .
CONCATENATE '[' p_str1 ']' INTO p_str1.
REPLACE ALL OCCURRENCES OF REGEX p_str1 IN lv_tmp WITH ''.
WRITE lv_tmp.
Read only

0 Likes
1,435

Your code is complex to understand because you change the input variables, so you could use supplementary variables.

So, you mean the result will be TEXT1234 - TEXT = 1234.

EDIT (thanks for correcting me): you mean the result will be TEXT1234 ∩ TEXT = TEXT

Note that your code doesn't take into account special characters like -, [, ], etc. You might insert \ before each character to avoid issues.

Read only

0 Likes
1,435
Sandra Rossi,This Code will Display TEXT as the result and not 1234, since only TEXT is the common character between the both. Yes you are right, we may not be able to compare Special Characters with this code.

Linda Yan,

You could try the below FM as well, this will handle special characters as well.

HR_GB_XML_PATTERN_CHECK

Regards
Read only

0 Likes
1,435
Linda Yan As per the SAP rules of engagement of SAP Community (forum), private information is considered as "unacceptable content" (no email, no confidential data, etc.)
Read only

Former Member
0 Likes
1,435

Check this code. str1 and str2 are string entered by user.

data: it type i value 0.
data: c(1) type char.

len1 = strlen( str1 ). "find length of first string

loop.
if (it >= len1 - 1).    " check if we are not excedding length of str1
exit.
else.
c = str1 + it(1) .    " find each character of str1
if str2 CS c.          " CS - contains, check if str2 contains c 
write:/ 'matching character' c 'at index' it .    " if contains do something here 
endif. 
endif.
it = it + 1.
endloop.
Read only

0 Likes
1,435

LOOP alone? Do you mean DO...ENDDO?