IT WORK/SAP FI and ABAP

[ABAP] 한국어,중국어,일본어 포함 여부 How to check if string has Korean, Chinese or Japanese (2 byte strings)

Bathildis 2023. 9. 4. 10:35
반응형
DATA: V_ABAP_ENCOD TYPE ABAP_ENCODING VALUE '4103',
      V_CPCODEPAGE TYPE CPCODEPAGE VALUE '4103'.
DATA: V_X2(2) TYPE X,
      V_XSTR TYPE XSTRING,
      V_XSTR2 TYPE XSTRING.


DATA: V_STR TYPE STRING,
      V_STR2 TYPE STRING.


*&---------------------------------------------------------------------*
*& SELECTION-SCREEN
*&---------------------------------------------------------------------*
PARAMETERS: P_TEXT TYPE STRING DEFAULT 'ABCD@#$會仔细观察FDGHERRGG*&<>找到DFDFDF'.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION
*&---------------------------------------------------------------------*
START-OF-SELECTION.

  "get codepage of the language, instead of hardcode the codepage
  CALL FUNCTION 'SCP_CODEPAGE_FOR_LANGUAGE'
  EXPORTING
    LANGUAGE  = sy-langu
  IMPORTING
    CODEPAGE  = V_CPCODEPAGE.

  V_ABAP_ENCOD = V_CPCODEPAGE.

  "print out the original input string
  WRITE: / 'Input string =', P_TEXT.

  "firstly convert the string to binary per language codepage
  v_xstr = CL_BCS_CONVERT=>STRING_TO_XSTRING( EXPORTING IV_STRING = P_TEXT  IV_CODEPAGE = V_ABAP_ENCOD ).

  CHECK V_XSTR IS NOT INITIAL.


  DO.
    V_X2 = V_XSTR.  "get 2 bytes of data in string (under unicode, each char stored by 2 bytes)
    V_XSTR2 = V_X2.

    "here just to convert back the binary back to string under same codepage
    "to test the conversion is correct
    V_STR = CL_BCS_CONVERT=>XSTRING_TO_STRING( EXPORTING IV_XSTR = V_XSTR2  IV_CP = V_CPCODEPAGE ).

    IF V_X2+1(1) = '00'.  "as ASCII only require 1 byte encoding, the 2nd byte always 00
      WRITE: / V_STR, '=>', V_X2, 'ASCII'.
    ELSE.
      WRITE: / V_STR, '=>', V_X2, 'Non-ASCII'.
    ENDIF.

    "here just to reconstruct the input string (string->binary->string) to check correctness
    V_STR2 = V_STR2 && V_STR.

    "shift 2 bytes for getting next character
    SHIFT V_XSTR BY 2 PLACES LEFT IN BYTE MODE.
    IF V_XSTR IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

"print out the reconstructed string
WRITE: / 'Reconstructed string =', V_STR2.

 

 

output

 

 

How to find whether char value is Chinese or not in the Unicode system? | SAP Community

반응형