Find out if a string contains only ASCII characters

后端 未结 3 1430
傲寒
傲寒 2020-12-06 15:07

I need to know whether a string contains only ASCII characters. So far I use this REGEX:

DECLARE
    str VARCHAR2(100) := \'xyz\';
BEGIN
    IF REGEXP_LIKE(s         


        
相关标签:
3条回答
  • 2020-12-06 15:29

    ASCII function with upper limit of 127 may be used :

    declare
        str nvarchar2(100) := '\xyz~*-=)(/&%+$#£>|"éß';
        a   nvarchar2(1);
        b   number := 0;
    begin
        for i in 1..length(str)
        loop                 
          a := substrc(str,i,1);
          b := greatest(ascii(a),b);      
        end loop;
    
        if b < 128 then  
         dbms_output.put_line('String is composed of Pure ASCII characters');
        else
         dbms_output.put_line('String has non-ASCII characters');      
        end if; 
    end;
    
    0 讨论(0)
  • 2020-12-06 15:31

    I think I will go for one of these two

    IF CONVERT(str, 'US7ASCII') = str THEN
        DBMS_OUTPUT.PUT_LINE('Pure ASCII');
    END IF;
    
    
    
    IF ASCIISTR(REPLACE(str, '\', '/')) = REPLACE(str, '\', '/') THEN
        DBMS_OUTPUT.PUT_LINE('Pure ASCII');
    END IF;
    
    0 讨论(0)
  • 2020-12-06 15:39

    You can use TRANSLATE to do this. Basically, translate away all the ASCII printable characters (there aren't that many of them) and see what you have left.

    Here is a query that does it:

    WITH input ( p_string_to_test) AS ( 
    SELECT 'This this string' FROM DUAL UNION ALL
    SELECT 'Test this ' || CHR(7) || ' string too!' FROM DUAL UNION ALL
    SELECT 'xxx' FROM DUAL)
    SELECT p_string_to_test, 
           case when translate(p_string_to_test, 
           chr(0) || q'[ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~]', 
           chr(0)) is null then 'Yes' else 'No' END is_ascii
    FROM input;
    
    +-------------------------+----------+
    |    P_STRING_TO_TEST     | IS_ASCII |
    +-------------------------+----------+
    | This this string        | Yes      |
    | Test this  string too!  | No       |
    | xxx                     | Yes      |
    +-------------------------+----------+
    
    0 讨论(0)
提交回复
热议问题