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: 

one character from string

Former Member
0 Kudos
5,070

Hi friends,

I have one string like 'HELLO' I want to take only one character from that string suppose ' E '.

How can i fetch one character from whole srting?

plz reply me.

1 ACCEPTED SOLUTION

Former Member
0 Kudos
529

Access Using Offset and Length Specifications

Offset and length specifications are generally critical since the length of each character is platform-dependent. As a result, it is initially unclear as to whether the byte unit or the character unit is referred to in mixed structures. This forced us to put in place certain considerable restrictions. However, access using offset or length specifications is still possible to the degree described in the following. The tasks subject to this rule include accessing single fields and structures, passing parameters to subroutines and working with field symbols.

Single field access

Offset- or length-based access is supported for character-type single fields, strings and single fields of types X and XSTRING. For character-type fields and fields of type STRING, offset and length are interpreted on a character-by-character basis. Only for types X and XSTRING, the values for offset and length are interpreted in bytes.

Structure access

Offset- or length-based access to structured fields is a programming technique that should be avoided. This access type results in errors if both character and non-character-type components exist in the area identified by offset and length.

Offset- or length-based access to structures is only permitted in a UP if the structures are flat and the offset/length specification includes only character-type fields from the beginning of the structure. The example below shows a structure with character-type and non-character-type fields. Its definition in the ABAP program and the resulting assignment in the main memory is as follows:

BEGIN OF STRUC,

a(3) TYPE C, "Length: 3 characters

b(4) TYPE N, "Length: 4 characters

c TYPE D, "Length: 8 characters

d TYPE T, "Length: 6 characters

e TYPE F, "Length: 8 bytes

f(26) TYPE C, "Length: 28 characters

g(4) TYPE X, "Length: 2 bytes

END OF STRUC.

Internally, the fragment view contains four fragments. Offset- or length-based access in this case is only possible in the first fragment. Statements like struc(21) or struc7(14) are accepted by the ABAP interpreter and treated like a single field of type C. By contrast, struc57(2) access is now only allowed in an NUP. If offset-/length-based access to a structure is permitted, both the offset and length specifications are generally interpreted as characters in a UP.

Passing parameters to subroutines

Up to now, parameter passing with PERFORM has allowed you to use cross-field offset and length specifications. In future, this will no longer be allowed in a UP. In a UP, offset and length-based access beyond field boundaries returns a syntax or runtime error. For example, access types c15 or c5(10) would trigger such an error for a ten-digit C field c.

If only an offset but no length is specified for a parameter, the entire length of the field instead of the remaining length was previously used for access. As a result, parameter specifications are cross-field if you use only an offset, and therefore trigger a syntax error in a UP. PERFORM test USING c+5 is consequently not permitted.

In addition, in a UP, you can continue to specify the remaining length starting from the offset off for parameters using the form field+off(*).

Ranges for offset and length access when using field symbols

A UP ensures that offset- or length-based access with ASSIGN is only permitted within a predefined range. Normally, this range corresponds to the field boundaries in case of elementary fields or, in case of flat structures, to the purely character-type starting fragment. Using a special RANGE addition for ASSIGN, you can expand the range beyond these boundaries.

Field symbols are assigned a range allowed for offset/length specifications. If the source of an ASSIGN statement is specified using a field symbol, the target field symbol adopts the range of the source. If not explicitly specified otherwise, the RANGE is determined as follows:

ASSIGN feld TO <f>.

In a UP, the field boundaries of field are assigned to <fs> as the range, where field is no field symbol.

ASSIGN <g> TO <f>.

<fs2> adopts the range of <fs1>.

ASSIGN elfeld+off(len) TO <f>.

In a UP, the field boundaries of the elementary field elfield are assigned to <fs> as the range.

ASSIGN <elfld>+off(len) TO <f>.

<fs> adopts the range of the elementary field <elfield>.

ASSIGN struc+off(len) TO <f>.

ASSIGN <struc>+off(len) TO <f>.

In a UP, the purely character-type starting section of the flat structures struc or <struc> determines the range boundaries.

If the assignment to the field symbol is not possible because the offset or length specification exceeds the range permitted, the field symbol is set to UNASSIGNED in a UP. Other checks such as type or alignment checks return a runtime error in a UP. As a rule, offset and length specifications are counted in characters for data types C, N, D, and T as well as for flat structures, and in bytes in all other cases.

Offset without length specification when using field symbols

Up to now, ASSIGN field+off TO <fs> has shown the special behavior that the field length instead of the remaining length of field was used if only an offset but not length was specified. Since an ASSIGN with a cross-field offset is therefore problematic under Unicode, you must observe the following rules:

As previously, the field length of field is used as the length. Using ASSIGN field+off(*)... you can explicitly specify the remaining length.

ASSIGN <fs1>+off TO <fs2> is only permitted if the runtime type of <fs1> is flat and elementary, that is, C, N, D, T (offset in characters), or X (offset in bytes).

ASSIGN field+off TO <fs2> is generally forbidden from a syntactical point of view since any offset <> 0 would cause the range to be exceeded. Exceptions are cases in which a RANGE addition is used.

These rules enable you also in future to pass a field symbol through an elementary field using ASSIGN <fs>+n TO <fs>, as it is the case in a loop, for example.

Use the categories used in the Dictionary when using these types with Structure Enhancements.

5 REPLIES 5

0 Kudos
529

Hi,

You can do like this

DATA: str type string value 'HELLO',

char.

char = str+1(1).

write: char.

Regards,

Sesh

Former Member
0 Kudos
530

Access Using Offset and Length Specifications

Offset and length specifications are generally critical since the length of each character is platform-dependent. As a result, it is initially unclear as to whether the byte unit or the character unit is referred to in mixed structures. This forced us to put in place certain considerable restrictions. However, access using offset or length specifications is still possible to the degree described in the following. The tasks subject to this rule include accessing single fields and structures, passing parameters to subroutines and working with field symbols.

Single field access

Offset- or length-based access is supported for character-type single fields, strings and single fields of types X and XSTRING. For character-type fields and fields of type STRING, offset and length are interpreted on a character-by-character basis. Only for types X and XSTRING, the values for offset and length are interpreted in bytes.

Structure access

Offset- or length-based access to structured fields is a programming technique that should be avoided. This access type results in errors if both character and non-character-type components exist in the area identified by offset and length.

Offset- or length-based access to structures is only permitted in a UP if the structures are flat and the offset/length specification includes only character-type fields from the beginning of the structure. The example below shows a structure with character-type and non-character-type fields. Its definition in the ABAP program and the resulting assignment in the main memory is as follows:

BEGIN OF STRUC,

a(3) TYPE C, "Length: 3 characters

b(4) TYPE N, "Length: 4 characters

c TYPE D, "Length: 8 characters

d TYPE T, "Length: 6 characters

e TYPE F, "Length: 8 bytes

f(26) TYPE C, "Length: 28 characters

g(4) TYPE X, "Length: 2 bytes

END OF STRUC.

Internally, the fragment view contains four fragments. Offset- or length-based access in this case is only possible in the first fragment. Statements like struc(21) or struc7(14) are accepted by the ABAP interpreter and treated like a single field of type C. By contrast, struc57(2) access is now only allowed in an NUP. If offset-/length-based access to a structure is permitted, both the offset and length specifications are generally interpreted as characters in a UP.

Passing parameters to subroutines

Up to now, parameter passing with PERFORM has allowed you to use cross-field offset and length specifications. In future, this will no longer be allowed in a UP. In a UP, offset and length-based access beyond field boundaries returns a syntax or runtime error. For example, access types c15 or c5(10) would trigger such an error for a ten-digit C field c.

If only an offset but no length is specified for a parameter, the entire length of the field instead of the remaining length was previously used for access. As a result, parameter specifications are cross-field if you use only an offset, and therefore trigger a syntax error in a UP. PERFORM test USING c+5 is consequently not permitted.

In addition, in a UP, you can continue to specify the remaining length starting from the offset off for parameters using the form field+off(*).

Ranges for offset and length access when using field symbols

A UP ensures that offset- or length-based access with ASSIGN is only permitted within a predefined range. Normally, this range corresponds to the field boundaries in case of elementary fields or, in case of flat structures, to the purely character-type starting fragment. Using a special RANGE addition for ASSIGN, you can expand the range beyond these boundaries.

Field symbols are assigned a range allowed for offset/length specifications. If the source of an ASSIGN statement is specified using a field symbol, the target field symbol adopts the range of the source. If not explicitly specified otherwise, the RANGE is determined as follows:

ASSIGN feld TO <f>.

In a UP, the field boundaries of field are assigned to <fs> as the range, where field is no field symbol.

ASSIGN <g> TO <f>.

<fs2> adopts the range of <fs1>.

ASSIGN elfeld+off(len) TO <f>.

In a UP, the field boundaries of the elementary field elfield are assigned to <fs> as the range.

ASSIGN <elfld>+off(len) TO <f>.

<fs> adopts the range of the elementary field <elfield>.

ASSIGN struc+off(len) TO <f>.

ASSIGN <struc>+off(len) TO <f>.

In a UP, the purely character-type starting section of the flat structures struc or <struc> determines the range boundaries.

If the assignment to the field symbol is not possible because the offset or length specification exceeds the range permitted, the field symbol is set to UNASSIGNED in a UP. Other checks such as type or alignment checks return a runtime error in a UP. As a rule, offset and length specifications are counted in characters for data types C, N, D, and T as well as for flat structures, and in bytes in all other cases.

Offset without length specification when using field symbols

Up to now, ASSIGN field+off TO <fs> has shown the special behavior that the field length instead of the remaining length of field was used if only an offset but not length was specified. Since an ASSIGN with a cross-field offset is therefore problematic under Unicode, you must observe the following rules:

As previously, the field length of field is used as the length. Using ASSIGN field+off(*)... you can explicitly specify the remaining length.

ASSIGN <fs1>+off TO <fs2> is only permitted if the runtime type of <fs1> is flat and elementary, that is, C, N, D, T (offset in characters), or X (offset in bytes).

ASSIGN field+off TO <fs2> is generally forbidden from a syntactical point of view since any offset <> 0 would cause the range to be exceeded. Exceptions are cases in which a RANGE addition is used.

These rules enable you also in future to pass a field symbol through an elementary field using ASSIGN <fs>+n TO <fs>, as it is the case in a loop, for example.

Use the categories used in the Dictionary when using these types with Structure Enhancements.

Former Member
0 Kudos
529

Hi Prashant,

Ex code:

Data str type string value 'HELLO'.

Data char type c.

char = str+pos(1).

u can give any position starting from 0 based up on the req

for getting E u can use value 1 for pos.

Hope this is helpfull.

Regards,

Ravi Kumar

Former Member
0 Kudos
529

Hello Prashant,

If you want a perticular character from a string...

Use the concept of OFFSET and INDEX.

Former Member
529

Hi Prashant,

Try the following code

DATA : str type string value 'HELLO',

v_char type c.

v_char = str+1(1).

write : v_char.

<b>analysis of str+1(1).</b>

here,

Str = the string that you want to process

first number = the position at which u want to read the character

for first character this offset will be 0,

for second character this will be 1, and so on

second number in bracket = the number of characters u want to read

according to ur query, str+1(1) means read one character at 2nd position in the string 'str'.

Regards,

Sonal

<b>Reward points if helpful</b>

Regards,

Sesh