07-02-2015 11:45 AM
Hello,
I am having a requirement. I am having str1 and str2. I need to display the characters that are not common in both the strings.
data: lv_key type string value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str type string value '&TES%T'.
lv_key has the set of values that are allowed. lv_str has the input string.
Now i need the display like "Invalid chars are &%" in my write statement. Guys i know we can check character by character. But that will be a huge performance impact.
Secondly if we use CN we will get the SY-FDPOS with the first invalid char only. in case ours has two and in real time it can have many.
Any better ideas ??
Thanks,
Karthik
07-02-2015 12:17 PM
Hi Karthikeyan,
Karthikeyan Chandrasekaran wrote:
Hello,
I am having a requirement. I am having str1 and str2. I need to display the characters that are not common in both the strings.
Karthik
If you need only not common characters, I hope you can use REPLACE ALL Statement as shown below.
DATA: lv_key TYPE string VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str TYPE string VALUE '&TES%T'.
CONCATENATE '[' lv_str ']' INTO lv_str.
REPLACE ALL OCCURRENCES OF REGEX lv_str IN lv_key WITH `` .
WRITE lv_key.
Hope it helps.
Regards
Rajkumar Narasimman
07-02-2015 12:00 PM
Hi Karthik.
If this is a real time process then surely the volume of entries would be quite small, therefore the performance of looping through the string should not be too intensive.
I cannot think of a slicker way to write the code, but there's far better developers on SCN than me so standby for a better response.
Regards
Arden
07-02-2015 12:13 PM
Hi Karthik,
As per your requirement, you have to check character by character only, but in different way.
Don't know, if some will have something better then this process. It could be.
DATA: lv_key TYPE string VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str1 TYPE string VALUE '&TES%T'.
DATA: lv_result TYPE string,
lv_res TYPE c,
lv_int TYPE i,
lv_pro TYPE i.
lv_int = 0.
lv_pro = 1.
IF strlen( lv_key ) GT strlen( lv_str1 ).
DO strlen( lv_str1 ) TIMES.
IF lv_str1+lv_int(lv_pro) CN lv_key.
CONCATENATE lv_result lv_str1+lv_int(lv_pro) INTO lv_result.
ENDIF.
lv_int = lv_int + 1.
ENDDO.
ELSE.
DO strlen( lv_key ) TIMES.
IF lv_key+lv_int(lv_pro) CN lv_key.
CONCATENATE lv_result lv_key+lv_int(lv_pro) INTO lv_result.
ENDIF.
lv_int = lv_int + 1.
ENDDO.
ENDIF.
Regards,
Praveer.
07-02-2015 12:17 PM
Hi Karthikeyan,
Karthikeyan Chandrasekaran wrote:
Hello,
I am having a requirement. I am having str1 and str2. I need to display the characters that are not common in both the strings.
Karthik
If you need only not common characters, I hope you can use REPLACE ALL Statement as shown below.
DATA: lv_key TYPE string VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str TYPE string VALUE '&TES%T'.
CONCATENATE '[' lv_str ']' INTO lv_str.
REPLACE ALL OCCURRENCES OF REGEX lv_str IN lv_key WITH `` .
WRITE lv_key.
Hope it helps.
Regards
Rajkumar Narasimman
07-02-2015 12:33 PM
Thanks Raj,
However, it replaces all the available text in key with space. its like ABCD FGHIJKLMNOPQR UVWXYXZ*#^
The output shud be &% .
07-02-2015 12:39 PM
Hi Karthikeyan,
Some small changes to Rajkumar's code.
DATA: lv_key TYPE string VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str TYPE string VALUE '&TES%T'.
CONCATENATE '[' lv_key ']' INTO lv_key.
REPLACE ALL OCCURRENCES OF REGEX lv_key IN lv_str WITH `` .
WRITE lv_str.
Regards,
Suhas
07-02-2015 12:40 PM
Hi Karthikeyan Chandrasekaran,
Find the below code, I think you can increase performance a bit more.
DATA: lv_key TYPE string VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ*#^',
lv_str TYPE string VALUE '&TES%T'.
DATA : lv_length1 TYPE i,
lv_length2 TYPE i,
lv_result TYPE string.
lv_length1 = strlen( lv_key ).
DO lv_length1 TIMES.
IF lv_key+lv_length2(1) CN lv_str.
IF lv_key+lv_length2(1) CN lv_result.
CONCATENATE lv_key+lv_length2(1) lv_result INTO lv_result.
ENDIF.
ENDIF.
ADD 1 TO lv_length2.
ENDDO.
lv_result = reverse( lv_result ).
WRITE lv_result.
Regards,
Omkar.
07-02-2015 2:22 PM
Hi Suhas,
That works ., i am getting a dump when i execute my original key
Could u replace data: lv_key type string value 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789- ./%'.
lv_str = 'ABC-B0004#60761'
i am getting CX_SY_INVALID_REGEX dump. Guess some characters are reserved and we cant use it.
let me know if you are successful.
Thanks,
Karthik
07-02-2015 3:06 PM
Thanks for all your replies.
we can use FM HR_GB_XML_PATTERN_CHECK for the same.
If we try using REGEX, and if it has some reserved or special characters, it will give a dump.
Thanks,
Karthik