Split a string at a number

Development (ABAP Development WorkBench, ABAP/4 programming)

Moderators: Snowy, thx4allthefish, YuriT, Gothmog

Post Reply
alucardme87
Posts: 2
Joined: Tue Jun 07, 2011 2:34 am

Split a string at a number

Post by alucardme87 » Tue Jun 07, 2011 2:43 am

Can someone help me out. I want to split a string at a 'number' position and then move all the splited parts into holder. LIke this

1 this is 2 an example 3 string field

I want to split at 2 and 3 and remove any space in the strings. The result will be like this:

1 thisis
2 anexample
3 stringfield

Can someone help me, please.

ese_pinche_ABAP
Posts: 297
Joined: Tue Mar 01, 2005 12:57 pm

Re: Split a string at a number

Post by ese_pinche_ABAP » Tue Jun 07, 2011 6:17 am

This sounds like a wonderful "Programming 101" exercise - it also sounds like something you should do for yourself and at least post YOUR efforts to solve it, before you ask US to spoon-feed you.

You are aware of the SPLIT command? And of the various string comparison operators and other string commands? There are probably a zillion ways to do this. You should come up with something on your own first. I would be ashamed to ask someone else to give me the answer to this before I tried on my own, even (or especially) many years ago when first I started to program.

YuriT
Posts: 885
Joined: Fri Feb 03, 2006 6:40 am
Location: Basel/Riga

Re: Split a string at a number

Post by YuriT » Tue Jun 07, 2011 6:28 am

SPLIT is not of real use to you here because you need to split at any number. Try FIND + MATCH OFFSET and then string(offset).
And yes, ese_pinche_ABAP is right. This is a simple task that you can do in hundreds of ways, you just have to try.

ese_pinche_ABAP
Posts: 297
Joined: Tue Mar 01, 2005 12:57 pm

Re: Split a string at a number

Post by ese_pinche_ABAP » Tue Jun 07, 2011 7:24 am

Actually I could do it using SPLIT, even recursively, but your suggestion Yuri is probably better... :P

Something along the lines of:

if the string contains any of <numbers 0-9>
assign that number to var1
SPLIT at var1

..and I'll leave the rest as an exercise to the reader, as the textbooks say! :roll:

alucardme87
Posts: 2
Joined: Tue Jun 07, 2011 2:34 am

Re: Split a string at a number

Post by alucardme87 » Wed Jun 08, 2011 12:22 am

Thank for all the help. I've done it.
I used string offset and loop to do it. It even worked too great to make me wonder why the hell couldn't i come up with it earlier.

Post Reply