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

ABAP function check string

Former Member
0 Likes
1,779

Hi,

I need a help to write ABAP function which get a string as parameter

The fuction returns true only if the string length is 7 & the string contain only one '-'.

Thx

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,137

Hi ,

Chk the following code.. it works acc to the requirement..


FUNCTION ZFM.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(W_STRING) TYPE  STRING
*"  EXPORTING
*"     REFERENCE(W_RESULT) TYPE  STRING
*"----------------------------------------------------------------------
data: w_len type i.
w_len = strlen( w_string ).

if w_len eq 7.
do.
  if w_string ca '-'.
    w_result = 'True'.
    replace '-' in w_string with space .
    if sy-index ge 2.
    w_result = 'False'.
    exit.
    endif.
  endif.
  check sy-index gt 2 .
  exit.
enddo.
else.
w_result = 'False'.
endif.

ENDFUNCTION.

Regards,

Mdi.Deeba

8 REPLIES 8
Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,137

Hello,

I dont think that there is any function available for this.

A few lines of code & thats it.

DATA:
V_TRUE TYPE FLAG,
V_LEN TYPE I,
V_LINES TYPE I.

CLEAR: V_LEN, V_LINES, V_TRUE.

V_LEN = STRLEN( V_INPUT ). 

SPLIT V_INPUT AT '-' INTO TABLE ITAB.

V_LINES = LINES( ITAB ).

"If true, then V_LEN = 7 & V_LINES = 2
IF V_LEN = 7 AND V_LINES = 2.
V_TRUE = 'X'.
ENDIF.

BR,

Suhas

Read only

Former Member
0 Likes
1,137

Thnak you

ITAB in not defined, please define it

Read only

0 Likes
1,137

Hi, Qwart

As i understand your requirement, you need the following, Test the code given bellow and revert me back if any else requirement.

PARAMETERS: string TYPE string.
DATA: ans(1).

PERFORM: check_string USING string ans.

WRITE: ans. " Here it will write T or F ( T for True and F for False )
*&---------------------------------------------------------------------*
*&      Form  check_string
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->VALUE(STR) text
*      -->FLAG       text
*----------------------------------------------------------------------*
FORM check_string USING value(str) flag.

  DATA: sub_str1 TYPE string,
        sub_str2 TYPE string,
        slen TYPE i,
        sub_slen1(7),
        sub_slen2(7).

  slen = STRLEN( str ).

  IF slen = 7.
    FIND FIRST OCCURRENCE OF '-' IN str.
    IF sy-subrc = 0.
      SPLIT: str AT '-' INTO sub_str1 sub_str2.
      FIND FIRST OCCURRENCE OF '-' IN sub_str2.
      IF sy-subrc = 0.
        flag = 'F'.
      ELSE.
        flag = 'T'.
      ENDIF.
    ELSE.
      flag = 'F'.
    ENDIF.
  ELSE.
    flag = 'F'.
  ENDIF.
ENDFORM.                    "check_string

Regards,

Faisal

Read only

Former Member
0 Likes
1,137

Hi,

From the below program logic you can create the FM

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

data: v_len type i,v_return(1) type c value 'F'.

parameters: P_string type string.

v_len = strlen( p_string ).

if len = 7.

if p_string CO '-'.

v_return = 'T'.

endif.

endif.

write : v_return,len.

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

Hope this will help you.

Regards,

Smart Varghese

Read only

Former Member
0 Likes
1,137

HI,

You may use this :-

data len type i.

data str type string .(String to be checked)

len = strlen( str ).

if len = 7.

FIND - IN SECTION OFFSET off OF

str

endif.

regards,

Prakash

Read only

0 Likes
1,137
data s1 type string value 'asdf-dfgd-234-asd'.
data: d1 type i,l1 type i.
d1 = strlen( s1 ).
DATA : res_tab TYPE MATCH_RESULT_TAB,
       res TYPE MATCH_RESULT.
find ALL OCCURRENCES OF '-' in s1 RESULTS  res_tab .

DESCRIBE TABLE res_tab LINES l1.
WRITE : 'length:', d1, ' occurance:', l1.
Read only

Former Member
0 Likes
1,137

Hi,

try this:



DATA: I_STRING TYPE STRING VALUE '-------'.
DATA: L_STRING TYPE I.
*
L_STRING = STRLEN( I_STRING ).
*
IF L_STRING = 7 AND I_STRING = '-------'.
  WRITE: 'OK'.
ELSE.
  WRITE: / 'ERROR'.
ENDIF.
 

regards, Dieter

Read only

Former Member
0 Likes
1,138

Hi ,

Chk the following code.. it works acc to the requirement..


FUNCTION ZFM.
*"----------------------------------------------------------------------
*"*"Local Interface:
*"  IMPORTING
*"     VALUE(W_STRING) TYPE  STRING
*"  EXPORTING
*"     REFERENCE(W_RESULT) TYPE  STRING
*"----------------------------------------------------------------------
data: w_len type i.
w_len = strlen( w_string ).

if w_len eq 7.
do.
  if w_string ca '-'.
    w_result = 'True'.
    replace '-' in w_string with space .
    if sy-index ge 2.
    w_result = 'False'.
    exit.
    endif.
  endif.
  check sy-index gt 2 .
  exit.
enddo.
else.
w_result = 'False'.
endif.

ENDFUNCTION.

Regards,

Mdi.Deeba