Oracle如何正确判断带中文的字符串长度

您所在的位置:网站首页 oracle字符串最大长度 Oracle如何正确判断带中文的字符串长度

Oracle如何正确判断带中文的字符串长度

2024-07-09 22:00| 来源: 网络整理| 查看: 265

  The "length" functions return the length of char.       LENGTH calculates length using characters as defined by the input character set.       LENGTHB uses bytes instead of characters.       LENGTHC uses Unicode complete characters.       LENGTH2 uses UCS2 codepoints.       LENGTH4 uses UCS4 codepoints

 

  length函数返回字符的长度,它使用定义好的输入的字符集计算长度.  lengthb使用bytes代替字符  VSIZE returns the number of bytes in the internal representation of expr.  vsize 返回内部表示的字节的数目。internal representation of expr谁能解释一下。

 

  看sql示例:  select length('adfad合理') "bytesLengthIs" from dual --7  select lengthb('adfad') "bytesLengthIs" from dual --5  select lengthb('adfad合理') "bytesLengthIs" from dual --11  select vsize('adfad合理') "bytesLengthIs" from dual --11  select lengthc('adfad合理')"bytesLengthIs" from dual --7

 

  结论:     在utf-8的字符集下  lengthb=vsize  lengthc=length

  疑问:中文字符怎么会占用了3个byte?而不是2个。是utf-8字符集的原因?

  谁知道??????

  参考文献:Oracle9i SQL Reference Release 2 (9.2)

  ..................................................

  用String的getBytes方法测试了一下.  结论是utf-8的中文字符占用3个字节,gbk的中文字符占用2个字节,iso-8859-1的中文字符被识别为占用2个字节,iso不支持中文字符的编码,应该是都当成某个拉丁字母了.Oracle没有关系,oracle只是负责存储数据.  可以先用 select * from v$nls_parameters 看看oracle的字符集

 

下边是测试的java类:

import java.io.UnsupportedEncodingException; public class TextEncoding { /** * * @author:sunflower * @date: 2007-1-24 上午10:09:40 * @todo: 调用的是String的自己的getBytes(encoding)方法, * 使用指定的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中. * @param content * @param encode * @return */ public static byte[] getBytes(String content,String charsetName) throws UnsupportedEncodingException{ return content.getBytes(charsetName); } /** * * @author:sunflower * @date: 2007-1-24 上午10:19:40 * @todo: 调用的是String的自己的getBytes()方法, * 使用平台默认的字符集将此 String 解码为字节序列,并将结果存储到一个新的字节数组中。 * @param content * @return */ public static byte[] getBytes(String content){ return content.getBytes(); } public static void main(String[]args){ String content="1e宝宝"; byte[] len; try{ len=getBytes(content,"UTF-8"); System.out.println(" the byte array length is "+len.length); len=getBytes(content,"GBK"); System.out.println(" the byte array length is "+len.length); len=getBytes(content,"ISO-8859-1"); System.out.println(" the byte array length is "+len.length); }catch(Exception e){ System.out.println("Can 't recognize"); } // System.out.println("the content byte[] length is "+); } }

 输出 :

the byte array length is 8 the byte array length is 6 the byte array length is 4

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3