2019 Oct 06 11:46 AM
Hi!
How to find common characters in two strings, when the user enters strings himself?
Hi!
How to find common characters in two strings, when the user enters strings himself?
2019 Oct 06 1:21 PM
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
2019 Oct 06 3:14 PM
Or eventually this one: https://answers.sap.com/questions/651427/algorithm-longest-common-subsequence-problem-in-ab.html
2019 Oct 06 5:48 PM
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.
2019 Oct 06 6:29 PM
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.
2019 Oct 07 6:42 AM
You could try the below FM as well, this will handle special characters as well.
HR_GB_XML_PATTERN_CHECK
Regards2019 Oct 08 11:11 PM
2019 Oct 06 6:53 PM
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.
2019 Oct 07 7:26 AM
| User | Count |
|---|---|
| 6 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |