04-03-2020 9:27 AM
Hey all,
let's say i have a string 'Test#String', and i want to split it at #, to get 'Test'.
The only way to do this that i found so far is this:
SPLIT var AT '#' INTO var DATA(DUMMY).
Is there a way (preferably Object Oriented) to do this without the DUMMY variable?
Thanks in advance
04-03-2020 12:20 PM
You can use string functions instead of the split command:
data(var) = `part1 # part2`.
var = segment( val = var sep = `#` index = 1 ).
" var = part1
04-03-2020 10:26 AM
Therefore, If you want to avoid declaring another variable and still have it in one statement, you can use replace and regular expressions, like this:
REPLACE REGEX '#.*' in var with space.
04-21-2020 9:11 AM
Hey there, thanks for your answer!
1. I would prefer an Object Oriented way, because i come from a Java background, and i like OO features like using a method as the parameter of another method call for example. Btw, is the answer from Lars Sawyer object oriented? Also, performance and memory overhead is not a problem in my case
3. I don't think it would be as well readable if i use replace, something like split would be easier to understand i think
04-21-2020 11:34 AM
>> 1. I would prefer an Object Oriented way, because i come from a Java background, and i like OO features like using a method as the parameter of another method call for example.
So you would 'always' rather also do mathematical operations with OO methods instead of built in functions?
sum = summand1 + summand2. " not good, since it is not object oriented?
sum = Math.add(summand1, summand2). " better, since it is based on a class method?
sum = summand1.add(summand2). " best, since it is based on a instance method?
>> 2. Btw, is the answer from Lars Sawyer object oriented?
'Maybe'. SEGMENT is a built-in function. It follows the coding style of a class method and it also throws message class errors that can be caught in case of input problems. So most likely, internally, it is declared like a class method, but you would have to ask the ABAP developers to know for sure probably.
>> Also, performance and memory overhead is not a problem in my case
Of course, it never is. Until it is 😉
>> 3. I don't think it would be as well readable if i use replace, something like split would be easier to understand i think
If it is easier for you to understand it this way, than do it this way. There are many roads that lead to Rome. Just try to understand your options, possible advantages and disadvantages and test it. Once you acquire knowledge about a function or method, it might be useful further down the road.
04-28-2020 2:25 PM
>> 1. No, i mean in a sense that i can use everything in an operand position (not sure if it is called like that)
Example:
var = REPLACE( VAL = var REGEX = '../' WITH = '').
SPLIT var AT '#' INTO var DATA(DUMMY)
I Have to use two lines, instead of using the REPLACE function inside the SPLIT.
>> 2.5. Yeah, but that is a problem for my future self ^^
Thanks for all your help btw!
04-03-2020 12:20 PM
You can use string functions instead of the split command:
data(var) = `part1 # part2`.
var = segment( val = var sep = `#` index = 1 ).
" var = part1
04-21-2020 10:44 AM
lars_sawyer suggests is intriguing but I'm not convinced it's the best way. It isn't as readable nor immediately comprehensible as SPLIT, I think.
These string operations have their place, but I'd stick with the the keyword SPLIT. To make it "OO", you can create your own functional method. But to my mind (I also program in JAVA) that's the equivalent of using:
int sum = add_two_numbers( 1, 2 );
int add_two_numbers( int a, int b) {
return a + b;
}
instead of
int sum = 1 + 2;
ABAP is a rich language with, as michael.piesche points out, far more basic language components than Java. I really think it is better programming practice to use them.
Btw - I'd use SPLIT var AT '#' INTO TABLE data(var_components). No dummy then!