ABAP 判断字符串中的繁体中文/简体中文

GB2312是简体中文的字符集,如果将含有繁体字的字符串转换成GB2312就会跳异常

DATA:
  codepage(4)   TYPE n,
  encoding(20)  TYPE c,
  li_len        TYPE i,
  str           TYPE string VALUE '中文測試123asdfsadf/-.',
  conout        TYPE REF TO cl_abap_conv_out_ce.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    external_name = 'GB2312'
  IMPORTING
    sap_codepage  = codepage
  EXCEPTIONS
    not_found     = 1
    OTHERS        = 2.

encoding = codepage.
conout = cl_abap_conv_out_ce=>create( encoding  = encoding ). "GB2312

TRY .

    li_len = STRLEN( str ).
    conout->write( data = str n = li_len ). "尝试进行编码
  CATCH cx_root.

    WRITE '包含繁体中文!'. "有异常,不能进行GB2312编码
    RETURN.
ENDTRY.
WRITE '无异常'.

 

DATA:
  codepage(4)   TYPE n,
  encoding(20)  TYPE c,
  li_len        TYPE i,
  str           TYPE string VALUE '中文测试123asdfsadf/-.',
  conout        TYPE REF TO cl_abap_conv_out_ce.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    external_name = 'GB2312'
  IMPORTING
    sap_codepage  = codepage
  EXCEPTIONS
    not_found     = 1
    OTHERS        = 2.

encoding = codepage.
conout = cl_abap_conv_out_ce=>create( encoding  = encoding ). "GB2312

TRY .

    li_len = STRLEN( str ).
    conout->write( data = str n = li_len ). "尝试进行编码
  CATCH cx_root.

    WRITE '包含繁体中文!'. "有异常,不能进行GB2312编码
    RETURN.
ENDTRY.

WRITE '无异常'.

 

 BIG5是繁体中文的字符集

DATA:
  codepage(4)   TYPE n,
  encoding(20)  TYPE c,
  li_len        TYPE i,
  str           TYPE string VALUE '中文测试123asdfsadf/-.',
  conout        TYPE REF TO cl_abap_conv_out_ce.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    external_name = 'BIG5'
  IMPORTING
    sap_codepage  = codepage
  EXCEPTIONS
    not_found     = 1
    OTHERS        = 2.

encoding = codepage.
conout = cl_abap_conv_out_ce=>create( encoding  = encoding ). "GB2312

TRY .

    li_len = STRLEN( str ).
    conout->write( data = str n = li_len ). "尝试进行编码
  CATCH cx_root.

    WRITE '包含简体中文!'. "有异常,不能进行GB2312编码
    RETURN.
ENDTRY.

WRITE '无异常'.

 

DATA:
  codepage(4)   TYPE n,
  encoding(20)  TYPE c,
  li_len        TYPE i,
  str           TYPE string VALUE '中文測試123asdfsadf/-.',
  conout        TYPE REF TO cl_abap_conv_out_ce.

CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
  EXPORTING
    external_name = 'BIG5'
  IMPORTING
    sap_codepage  = codepage
  EXCEPTIONS
    not_found     = 1
    OTHERS        = 2.

encoding = codepage.
conout = cl_abap_conv_out_ce=>create( encoding  = encoding ). "GB2312

TRY .

    li_len = STRLEN( str ).
    conout->write( data = str n = li_len ). "尝试进行编码
  CATCH cx_root.

    WRITE '包含简体中文!'. "有异常,不能进行GB2312编码
    RETURN.
ENDTRY.

WRITE '无异常'.

 

posted @ 2024-02-01 13:56  做梦都笑醒  阅读(51)  评论(0编辑  收藏  举报