Regular expression can be used here.
Run this snippet.
- DATA: str TYPE char100 VALUE '10001:M :1001: :NN',
- results TYPE match_result_tab,
- result TYPE match_result,
- off TYPE i,
- len TYPE i.
- FIND ALL OCCURRENCES OF REGEX '([:,]) +' IN str RESULTS results.
- LOOP AT results INTO result.
- off = result-offset.
- len = result-length.
- TRANSLATE str+off(len) USING ' _'.
- ENDLOOP.
- WRITE str.
Here I have chosen 2 delimiters and placed inside [] . Spaces followed by them will be replaced by underscore. You can modify the list of delimiters.
After finding all the valid patterns or substrings, translate statement is used for replacing spaces.
For more information about regular expressions, see keyword documentation and run program DEMO_REGEX_TOY.
/.