c#获取字符串长度

  /// 
       /// 获取中英文混排字符串的实际长度(字节数)
       /// 
       /// 要获取长度的字符串
       /// 字符串的实际长度值(字节数)
       public long getStringLength(string str)
       {
           if (str.Equals(string.Empty))
               return 0;
           long strlen = 0;
           ASCIIEncoding strData = new ASCIIEncoding();
           //将字符串转换为ASCII编码的字节数字
           byte[] strBytes = strData.GetBytes(str);
           for (int i = 0; i <= strBytes.Length - 1; i++)
           {
               if (strBytes[i] == 63)  //中文都将编码为ASCII编码63,即"?"号
                   strlen++;
               strlen++;
           }
           return strlen;
       }

       ///   
       /// 计算文件大小函数(保留两位小数),Size为字节大小  
       ///   
       /// 初始文件大小  
       ///   
       public string CountSize(long Size)
       {
           string m_strSize = "";
           long FactSize = 0;
           FactSize = Size;
           if (FactSize < 1024.00)
           {
               //m_strSize = (Math.Round(FactSize / 1024.00,3)).ToString() + " K";
               m_strSize = FactSize.ToString() + " Byte";
           }
           else if (FactSize >= 1024.00 && FactSize < 1048576)
               m_strSize = (FactSize / 1024.00).ToString("F2") + " K";
           else if (FactSize >= 1048576 && FactSize < 1073741824)
               m_strSize = (FactSize / 1024.00 / 1024.00).ToString("F2") + " M";
           else if (FactSize >= 1073741824)
               m_strSize = (FactSize / 1024.00 / 1024.00 / 1024.00).ToString("F2") + " G";
           return m_strSize;
       }

你可能感兴趣的:(c#获取字符串长度)