1、int Compare(); 区分大小写比较两个字符串,相等返回0,大于返回1,小于返回-1;
例:str1 = "abc123";
str2 = "ABC123";
str1.Compare(str2); //1 小写字母ASCII大于大写字母,所以str1大于str2
2、int CompareNoCase(); 不区分大小写比较两个字符串,相等返回0,大于返回1,小于返回-1;
例:str1 = "abc123";
str2 = "ABC123";
str1.CompareNoCase(str2); //0
3、int delete(int nIndex,int iCount); 删除字符,从nIndex开始的iCount个字符; 返回被删除前字符串的长度。
例:CString str = "abc123";
int i = str.delete(str.GetLength()-3,3) ; // str("abc") i=6;
//当nIndex过大,超出对像所在内存区域时,函数没有任何操作。
//当nIndex为负数时,从第一个字符开始删除。
//当nCount过大,导致删除字符超出对像所在内存区域时,会发生无法预料的结果。
//当nCount为负数时,函数没有任何操作。
4、void Empty( ) ; //清空字符串
例:CString str = "ABCD";
str.Empty(); //str("")
5、int Find() // 找到字符或字符串在原字符串中的第一个位置,不匹配返回-1
例:CString str = "abecde"
int i = str.Find('e'); //i = 2;
int a = str.Find('e',3); //i = 5; 从第四个字符串开始找,直到找到第一个字符
6、int FindOneOF(LPCTSTR m) //从字符串m中从左往右取字符,然后在原字符串中查找,有则返回位置
例:CString str = "aaaaef";
int i = str.FindOneOF("ae"); //i = 0;
7、TCHAR GetAt(int) //从字符串中取字符
例: CString str = "abcdef";
char a = str.GetAt(3); //a = 'd'
8、int GetLength( ) ; 返回字符串的长度,不包含结尾的空字符。
例:str="ABCDEF中文123456";
printf("%d",str.GetLength()); //16
9、int Insert(); 从指定位置开始插入字符串,返回新字符串的长度
例:int i = str1.Insert(-1,str2) //str1="ABC123abc123" i长度12
//当插入位置为负数时,从头开始插入
//当插入位置超过原字符串长度时,从尾部开始插入
10、bool IsEmpty() //判断字符串是否为空,如果是返回0,不是返回非0
11、CString Left(int n) //获取字符串左边的n个字符形成新字符串
12、void MakeReverse(); //颠倒字符串的顺序
例:str="ABCDEF中文123456";
str.MakeReverse(); //654321文中FEDCBA
13、void MakeUpper(); //将小写字母转换为大写字母
例:str="abc123";
str.MakeUpper(); //"ABC123"
14、void MakeLower(); //将字符串中大写字母全换成小写字母
15、CString Mid(); //从字符串的某个位置开始截取,如果有第二个参数,则为截取长度,没有截取到末尾
16、int Remove(TCHAR ) //移除字符串中指定的字符,返回移除掉的字符的个数
例:str1 = "abc123";
int i = str1.Remove('a'); //str1("bc123") i = 1;
17、int Replace(LPCTSTR strOld,LPCTSTR strNew); //替换strOld为strNew,返回替换字符串的个数.
例:str1 = "abc123";
int i = str1.Replace(“ab","AB"); //str1("ABc123") i = 1;
18、int ReverseFind(TCHAR) //从字符串末尾开始查找字符,并返回其索引值
19、CString Right(int n) //获取字符串的右边n个字符,并形成字符串返回
20、void SetAt(int n,TCHAR ch) //替换字符串的第n个字符为ch
21、void TrimLeft() //从字符串的左边开始,如果没有参数,就只清空空格、换行跟制表符,如果有参数(字符串),则从原字符串的左边开始清场参数字符串中出现的字符,直到遇到第一个不是参数字符串中的字符为止。
例:CString str = "abbcadfabc";
str.TrimLeft("ab") ; //str = "cadfabc";
22、void TrimRight() //同TrimLeft();不过是从右边开始清除
http://www.cnblogs.com/Caiqinghua/archive/2009/02/16/1391190.html
CString 常使用的成员函数及范例
最新推荐文章于 2022-10-18 21:16:38 发布