Application Development and Automation 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: 
Read only

Position of Character in a string

Former Member
0 Likes
64,822

Hi all,

Consider the string ,

" Hello World"

where the space before the letter H in Hello are empty character spaces. Can anyone tell me how I can find the position of the letter H from the beginning of the sentence using String processing ?

Thank you.

1 ACCEPTED SOLUTION
Read only

Former Member
20,459

Hi Ezhil,

DATA:

zlv_offset type i,

zlv_test type string value ' Hello World'.

find FIRST OCCURRENCE OF 'H' in zlv_test match OFFSET zlv_offset.

write zlv_offset.

Kind regads,

John.

hi,

SEARCH string FOR 'H'.

sy-fdpos will tell you where is it in the string.

ec

11 REPLIES 11
Read only

Former Member
0 Likes
20,459

Hi,

Use search

for example.

search space in string.

if sy-subrc eq 0.

w_counter = sy-fdpos + 1.

endif.

write: w_counter.

w_counter gives the position.

Plzz reward points if it helps.

Read only

JozsefSzikszai
Active Contributor
0 Likes
20,459

hi,

SEARCH string FOR 'H'.

sy-fdpos will tell you where is it in the string.

ec

Read only

ThomasZloch
Active Contributor
0 Likes
20,459

it can be done like this:

lf_string = ' Hello World'.
IF lf_string CA 'H'.
  "here, SY-FDPOS contains the position of the letter "H", in this case 2
ENDIF.

Greetings

Thomas

Read only

Former Member
20,460

Hi Ezhil,

DATA:

zlv_offset type i,

zlv_test type string value ' Hello World'.

find FIRST OCCURRENCE OF 'H' in zlv_test match OFFSET zlv_offset.

write zlv_offset.

Kind regads,

John.

Read only

Former Member
0 Likes
20,459

Hi,

Use SY-FDPOS system variable to find out the position of string.

SY-FDPOS

Location of hit in string operations.

When you use CO,CN, CA, NA, CS, NS, CP, and NP, offset values are assigned to SYFDPOS

depending on the search result.

SEARCH … FOR … sets SY-FDPOS to the offset of the search string.

Example:

DATA: STRING(30) VALUE 'This is a fast first example.',

POS TYPE I,

OFF TYPE I.

WRITE / STRING.

SEARCH STRING FOR 'ft' ABBREVIATED.

WRITE: / 'SY-FDPOS:', SY-FDPOS.

POS = SY-FDPOS + 2.

SEARCH STRING FOR 'ft' ABBREVIATED STARTING AT POS AND MARK.

WRITE / STRING.

WRITE: / 'SY-FDPOS:', SY-FDPOS.

OFF = POS + SY-FDPOS -1.

WRITE: / 'Off:', OFF.

Regards,

Bhaskar

Read only

Former Member
0 Likes
20,459

DATA text(30) value '  Hello world'.

SEARCH text FOR 'H'.
WRITE SY-FDPOS. "your case it returns 1



Read only

Former Member
0 Likes
20,459

hi,

This code below will help you to find the position of a character in a string.

PARAMETERS: INPUT(30) TYPE C,

FIND(1) TYPE C.

SEARCH INPUT FOR FIND.

IF SY-SUBRC = 0.

WRITE : 'CHAR FOUND'.

WRITE : SY-FDPOS.

ELSE.

WRITE : 'CHAR NOT FOUND'.

ENDIF.

Reward points if useful.

Read only

Former Member
0 Likes
20,459

Thank you all. The answers were very helpful.

Read only

Former Member
0 Likes
20,459

But I would also like to know how I can find the position of the character in the string if the String is dynamically generated. Can anyone throw some light on this ?

Read only

0 Likes
20,458

Hi,

In dynamic string also use search only.

for example

DATA: text TYPE string VALUE `Roll over Beethoven`,

pos TYPE i.

SEARCH text FOR '. .'.

SEARCH text FOR ` `.

IF sy-subrc = 0.

pos = sy-fdpos + 2.

SEARCH text FOR 'bth' STARTING AT pos

ABBREVIATED AND MARK.

ENDIF.

Plzz reward points if it helps.

Read only

Former Member
0 Likes
20,458

data: str type string .

data: l type i,

counter type i.

str = ' Hello World'.

l = strlen( str ).

write: l.

search str for 'H'.

if sy-subrc = 0.

counter = sy-fdpos + 1.

endif.

write: counter.