Application Development 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: 

Object Oriented way to Split Strings at certain Character

Former Member

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

1 ACCEPTED SOLUTION

lars-sawyer
Explorer

You can use string functions instead of the split command:

data(var) = `part1 # part2`.
var = segment( val = var sep = `#` index = 1 ).
" var = part1
6 REPLIES 6

michael_piesche
Active Contributor
  1. What do you mean by "preferably Object Oriented", because the task you are trying to achieve is on basic code statement level. OO will not achieve that for you by itself. It is up to you, to wrap that functionality either in OO-classes/instances-methods, FMs or even Report-Forms.
    Or are you hoping to find already existing SAP standard classes that provide that 'simple' task like yours already? I personally wouldnt waste the time looking for something like that, because you can implement it quicker than looking for something out there and getting familar with it, and having to deal with the overhead in terms of memory and performance as well.
  2. If you use the SPLIT statement, it has to do what you ask it to do, and that is SPLITTING one thing into two or more separate things. So you have to at least give the statement two variables to split something into (or use a table to split into). The SPLIT statement is preferably used, when you still need all the subelements that have been created after the split.
  3. But you actually dont want to 'split', you want to "find and replace" (see below coding).
    And also, you could solve the problem in other ways as well, that 'might' be also more 'performant', but will result in 'more' coding (like stepping through the positions of the variable and comparing it to the search criteria '#' and deleting it and everything else once it is found)
    I think if you can do it in one statement, I wouldnt try to figure out how to do it in another way.

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.

0 Kudos

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

Former MemberIn JAVA 'almost everything' is Object Oriented, except for example the basic predefined data type or basic mathematical operations, whereas ABAP did not start out as OO but has seen different stages of OO becoming more and more relevant. Therefore, you will not able to do EVERYTHING in OO context in SAP.

>> 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.

0 Kudos

>> 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!

lars-sawyer
Explorer

You can use string functions instead of the split command:

data(var) = `part1 # part2`.
var = segment( val = var sep = `#` index = 1 ).
" var = part1

matt
Active Contributor

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!