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: 

Split String At Index Position?

Former Member
0 Kudos

I have searched the documentation but I can't find what I'm looking for.

I have a string which i want to split into 2 strings at position x.

For example, splitting 'abcdef' at position 3 (if the index starts at 1) would result in two strings: 'ab' and 'cdef'. It doesn't matter to me if position 3 gets put in the first or second string.

Thanks.

9 REPLIES 9

Former Member
0 Kudos

Use FM <b>TEXT_SPLIT</b>

Rishi

Former Member

Can't you simply use offsets?

v_source_string = 'abcdef'.

v_first_split_string = v_source_string+0(2).

v_second_split_string = v_source_string+3.

Srinivas

0 Kudos

I'm sorry, my question doesn't accurately reflect my problem.

I actually need to remove the LAST x number of characters from my string.

I have string '_abcdefg_ _ _ ' (note there is one space at the beginning and 4 spaces at the end) and I want to remove the last 3 characters. Resulting in the string 'abcdefg_'. I want that first last space to be there (so CONDENSE won't help).

Any ideas?

THANKS!

0 Kudos

Hi,

You can try it this way:

First determine the length of your string. May be 10 or 15.

Then get the first 10-3 (or n-3 or n-k, where k is the last number of digits which u need to delete)characters of that string.

Also check out the FM:

STRING_SPLIT

Regards,

Anjali

0 Kudos

Hi Audrey,

The following code will do what you need. But is this always the case with your data? There may be better solution if you can specify what your actual requirement is. May be you are trying to display or list a large string with word wrap?

data:

source_string type string,

result_string type string,

null_string type string,

result_string = source_string.

condense result_string.

concatenate null_string result_string into result_string separated by space.

concatenate result_string null_string into result_string separated by space.

Cheers,

Ramki.

0 Kudos

Anjali,

How can I find the length of my STRING? I found documentation on finding lengths of character strings but I can't use it because i'm using string like this:

DATA: text TYPE string.

Thanks!

0 Kudos

Hi Audrey:

To achieve what you need, you can make use of NUMOFCHAR. Following example can provide a good understanding:

*****************************

DATA: my_string TYPE string.

DATA: my_string_length TYPE i.

my_string = 'This is a test string'.

my_string_length = NUMOFCHAR( my_string ).

WRITE: / my_string_length.

********************************************

Hope it helps.

Regards,

Chetan Singh

0 Kudos

Hi Audrey,

You can use the function STRLEN for finding the length of a string. I have attached the help file for your refrence.

STRLEN

Length of a string (type STRING or XSTRING) or a character field (particularly type C) up to its last non-space character (its occupied length).

Function expressions consist of three elements:

Function name with opening parenthesis (no space between)

Argument

Closing parenthesis

All elements of an expression, especially all elements of a function expression, must be separated from each other by at least one space.

Example

The following statements and the calculations they contain are syntactically correct:

DATA: I1 TYPE I, I2 TYPE I, I3 TYPE I,

F1 TYPE F, F2 TYPE F,

WORD1(10), WORD2(20),

XSTR TYPE XSTRING.

...

F1 = ( I1 + EXP( F2 ) ) * I2 / SIN( 3 - I3 ).

COMPUTE F1 = SQRT( SQRT( ( I1 + I2 ) * I3 ) + F2 ).

I1 = STRLEN( WORD1 ) + STRLEN( WORD2 ).

I2 = STRLEN( XSTR ).

Regards,

Anjali.

0 Kudos

Hi,

You can try following code in order to cut last 5 chars from string:

DATA: STRING(15),

LEN TYPE I.

LEN = STRLEN( STRING ).

LEN = LEN - 5.

IF LEN > 0.

STRING = STRING(LEN).

ENDIF.

Krzys