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: 

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

Former Member
0 Kudos

I have the text with sometimes with "English Characters" or "English + Chinese + Japense Characters" or "Chinese + Asian Character" etc.From my program, I would like to find whether Chinese characters are exist in the text or not. Is there any standard FM for this?

Of course, we can do the programming to find the Chinese character but tedious job. Because Chinese code page contains English characters as well as there is no standard range to identify the Chinese Unicode ( as some Chinese characters used in Japanese / Korean etc ) like we have ASCII Code for English and European characters.

Let me know if you have any quick and easy solution. Appreciate for the help

Regards,

Siva

4 REPLIES 4

Former Member
0 Kudos

Hii

chk this code

Each chinese character will take two bytes where as english character takes one byte. Becuase UOM is just 3 characters, try this way

select UOM from VBAP into v_UOM where <condition>

Here v_UOM should be atleast 6 characters. You have to change the code according to your requirement

SELECT * FROM T006A

WHERE MSEHI = < MEINS >

AND SPRAS = < CHINESE LANGUAGE >

Regards

Naresh

0 Kudos

Thanks.For different langauages, different UOMs are maintained in the table. So it works.

What I am asking is how to find the whether character has english / chinese / Janpense value?

For Example take the following text : "&#20170;XT&#27683;'. This text is entered by user on Selection Scree. Text contains english and chinese characters. In the program, I have to throw a simple message if I find the chinese character in the text. How do we do that ? Is there any easy solution like Standard SAP FMs ??? Please read my initial e-mail for clear picture.

Thanks in Advance for the help.

/Siva

Regards,

Siva

0 Kudos

Hi Again,

I have typed chinese characters in the following text "&#20170;XT&#27683;" of my earlier response but unfortunately this page is not supporting the chinese characters for display.

Regards,

Siva

kevin_chan5
Explorer
0 Kudos

I recently facing the same question, "how to detect Chinese characters inside a string in Unicode system".

Searched around and found that it seems impossible (unless, under a particular codepage, you store the encoding for each Chinese characters and do a match for each character in the string).

Finally I used a workaround which is good for my case: actually I don't need to know it's a Chinese character or not, I just need to know if the character is not under ASCII (encoded by 1 byte) but require two bytes encoding.

Here's my workaround:

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: