CString类常用方法---GetLength(),strlen(),IsEmpty(),Empty()

来源:互联网 发布:数据分析基础 教材 编辑:程序博客网 时间:2024/05/22 04:24

int GetLength( ) const;   //得到字符串的字节数(并不都是个数,如汉字一个字占两个字节)

int strlen( const char *string ); //计算参数字符串里面字节的个数并返回

BOOL IsEmpty( ) const; //如果字符串含有字符就返回假,如果是个空字符串就返回真

void Empty( );                 //清空字符串,相当于把字符串的值赋为""

注:

     在函数后面加 const 的意思是:

     如果一个类声明了一个常量对象,这个对象只能使用后边带 const 这个的方法.

例1:

CString a,b,c,d;
a = "123456789";
b = "中国人";
c = "中国人12345";

a.GetLength(); //得到9个
b.GetLength(); //得到6个
c.GetLength(); //得到11个

例2:

CString a;
a = "123456789";

if (TRUE == a.IsEmpty()) //如果a为空则把 "null_1" 打印在对话框中
{
  MessageBox("null_1");
}

a.Empty();                       //把a中的字符串清空

if (TRUE == a.IsEmpty()) //如果a为空则把 "null_2" 打印在对话框中
{
  MessageBox("null_2");
}

0 0