char*、string、CString的应用及相互转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/w_x_myself/article/details/82687102

目录

1、char*

1.1、简介

1.1、常用函数

1.1.1、初始化方法

1.1.2、获取字符(从键盘或者从文件中)

1.1.3、 输出

 1.1.4、字符串长度

1.1.5、字符串追加

 1.1.6、字符串比较

1.1.7、字符串拷贝

 1.1.8、格式化字符串

 1.1.9、sscanf格式化输出

 1.1.10、查找

1.1.11、 分割字符串

 

2、string类

2.1、简介

2.2、常用函数

2.2.1、构造函数

2.1.2、下标操作

2.1.3、获取相关属性(容量、长度、是否为空)

2.1.4、连接

2.1.5、比较

 2.1.6、截取、交换

2.1.7、查找

2.1.8、替换

2.1.9、插入

2.1.10、删除

3、CString类

3.1简介

3.2、常用函数

3.2.1、CString类对象的初始化(定义)

3.2.2、获取CString类数据长度

3.2.3、字符(串)的位置查找方法

3.2.4、CString类下标操作

3.2.5、对CString的截取

3.2.6、将数据按格式化输入到CString中(可用于int转CString)

3.2.7、CString的清空和判断是否为空

3.2.8、对CString进行颠倒、大小写转换操作

3.2.9、比较操作

3.2.10、CString类的删除、插入、替换操作

3.2.11、其他

4、类型转换

 


 

1、char*

1.1、简介

C语言中没有字符串这种数据类型,可以通过char的数组来替代,字符串就是以0结尾的char的数组

如果有一个char的数组,但不是以0结尾的,那么这个数组就不是一个字符串,所以字符串是一种特殊的char的数组

 一个char的数组中可以出现多个0,但一个字符串中只能1个0

1.1、常用函数

1.1.1、初始化方法

char c[5] = "1234";
char c1[5] = { '1', '2', '3', '4', '5' };
char c2[5] = { '1', '2', '3', '4'};
char c3[]="12345";
cout << c << endl;//1234
cout << c1 << endl;//12345乱码
cout << c2 << endl;//1234,默认以‘\0’结尾
cout << c3 << endl;//12345

1.1.2、获取字符(从键盘或者从文件中)

char s[5];
gets(s);//输入的数据超出数组的内存空间,会发生内存溢出。
cout << s << endl;
char s1[5];
	
fgets(s1, sizeof(s1), stdin); //参数1:数据储存位置,参数2:储存的数据大小,参数3:获取源(键盘或者文件)
cout << sizeof(s1) << endl;
cout << s1 << endl;

1.1.3、 输出

char s[] = "12345";
puts(s);//在最后自动添加一个’\n’,puts不支持各种转义字符,比如%d,%s都不支持,puts只能简单的直接输出一个字符串,而不能输出char,int,double等其他类型。
fputs(s, stdout);//参数2:指定数据输出位置(可以为文件或屏幕)。

 1.1.4、字符串长度

char s[] = "123456";
char s1[5] = "123";
cout << strlen(s)<<endl;//6
cout << sizeof(s) << endl;//7
cout << strlen(s1) << endl;//3
cout << sizeof(s1) << endl;//5

1.1.5、字符串追加

	char s[] = "12345";
	char s1[] = "67890";
	//strcat(s, s1);//1234567890,此处会报异常
	cout << s << endl;
	char s2[11] = "12345";
	char s3[11] = "67890";
	strcat(s2, s3);
	cout << s2 << endl;//1234567890
	char s4[8] = "12345";
	char s5[11] = "67890";
	strncat(s4, s5,2);//参数3:表示在从s5中截取前2个字符追加在s4末尾
	cout << s4 << endl;//1234567

 1.1.6、字符串比较

char s[] = "123456";
char s1[] = "123654";
cout << strcmp(s, s1) << endl;//-1
cout << strncmp(s, s1,3) << endl;//0,参数3:表示前3个字符进行比较

1.1.7、字符串拷贝

char s[10];
char s1[] = "123456";
char s2[] = "abcdef";
strcpy(s, s1);
cout << s << endl;//123456
strncpy(s, s2,3);
cout << s << endl;//adc456,参数3:表示从s2中拷贝3个字符到s中

 1.1.8、格式化字符串

char s[10]="dcdef";
int a = 10;
sprintf(s, "a%d", a);
cout << s << endl;//a10

 1.1.9、sscanf格式化输出

char s[10];
char s1[10];
sscanf("abcdef1234","%[a-z]", s);
cout << s << endl;//abcdef
sscanf("1234abcdef", "%[^a-z]", s1);
cout << s1 << endl;//1234

 1.1.10、查找

//发挥查找数据后面的字符串
char s[] = "1234567890";
cout << strchr(s, '6') << endl;
cout << strstr(s, "3456") << endl;

1.1.11、 分割字符串

char buf[] = "abc@defg@igk";
	char *p = strtok(buf, "@");
	while (p)
	{
		cout << p << endl;//1、abc   2:def    3:igk
		p = strtok(NULL, "@");
	}

 

2、string类

2.1、简介

string是C++标准程序库中的类,是因为他和char*比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个泛型类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。我们可以用 = 进行赋值操作,== 进行比较,+ 做串联(是不是很简单?)。我们尽可以把它看成是C++的基本数据类型。

为了在我们的程序中使用string类型,我们必须包含头文件 。如下:

#include<sting> //注意这里不是string.h string.h是C字符串头文件

using namespace std;  //此语句必不可少,否则有的编译器无法识别

2.2、常用函数

类的声明

 string str;  //生成一个空字符串s

2.2.1、构造函数

1、运用拷贝构造函数

构造参数可以为:string类、char*、字符数组。

char* ptr = NULL;
ptr = "ptr";
char ch[10] = { 'c', 'h', 'a', 'r', 'n', 'u', 'm' };
string str="abcdef";
string str1(str);
cout << str1 << endl;
string str2(ptr);
cout << str2 << endl;
string str3(ch);
//str3(ch);  为错误用法
cout << str3 << endl;

2、将string类的str从stridx位置开始到末尾的部分作为str1构造参数

 string str1(str,stridx)

string str="abcdef";
string str1(str, 1);
cout << str1 << endl;//bcdef

注:如果str为指针或者字符数组,只是将下标位置为1的值作为str1的构造参数

3、将字符串str位于stridx的位置开始,ncout个字符作为s的构造参数

string str3("12345678",2,3);//参数1类型可以为字符串、字符数组、字符指针
cout << str3 << endl;//345

4、生成一个包含num个c字符的字符串
string s(num,c) 

string str5(10, 'b');
cout << str5 << endl;

2.1.2、下标操作

string str="abcde";   
ch = str[3];//operator[]返回当前字符串中第n个字符的位置,无法使用try捕获异常   
cout << ch << endl; 

string str2 = "abcde";  
ch = str2.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常! 可以运用try捕获异常 
cout << ch << endl; 

2.1.3、获取相关属性(容量、长度、是否为空)

string str7 = "12345678901234567890";  
int size;  
size = str7.capacity();//返回当前容量:31   
cout << size << endl;   
size = str7.max_size();//返回string对象中可存放的最大字符串的长度 :-2  
cout << size << endl;   
size = str7.size();//返回当前字符串的大小:20
cout << size << endl;   
size = str7.length();//返回当前字符串的长度:20
cout << size << endl;   
bool flag;  
flag = str7.empty();//判断当前字符串是否为空:为空返回1;不为空返回0   
cout << flag << endl;  


2.1.4、连接

string str9=“123456”;
char *s = "ABCD";
string str10=“abcdefg”; 
str10 += str9;//把字符串str9连接到当前字符串的结尾:123456abcdefg
cout<<str10 << endl;  
str10.append(s);//把c类型字符串s连接到当前字符串的结尾:123456abcdefgABCD  
cout<<str10 << endl;   
str10.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾:123456abcdefgABCDAB  
cout<<str10 << endl;   
str10.append(str9.begin(), str9.end());//把迭代器之间的一段字符连接到当前字符串的结尾 :123456abcdefgABCDAB123456  
cout<<str10 << endl;   
str10.push_back('k');//把一个字符连接到当前字符串的结尾::123456abcdefgABCDAB123456k   
cout<<str10 << endl; 

2.1.5、比较

//直接运用>、<等运算符进行比较
string str="abcd";
string str1="ABCD"
bool flag;
flag = (str>str1);//判断两个字符串是否相等   
cout << flag << endl; 

string str2 = "abcdABCD";
string str3 = "ABCDabcd";
//通过compare函数比较,判断是否相等
flag = str2.compare(str3);//比较两个字符串的大小,通过ASCII的相减得出!   
cout << flag << endl;   
flag = str2.compare(4, 4, str3);//比较str10字符串从6开始的12个字符组成的字符串与str9的大小   
cout << flag << endl;  
flag = str2.compare(1, 4, str3, 4, 4);//比较str10字符串从6开始的12个字符组成的字符串与str9字符串从3开始的5个字符组成的字符串的大小   
cout << flag << endl; 

 2.1.6、截取、交换

string str = "1234567890abcdefgh";
string str1;
str1= str.substr(10, 2);//返回从下标10开始的2个字符组成的字符串 :ab  
cout << str1 << endl;
string str2="321";
str2.swap(str);
cout << str2 << endl;//1234567890abcdefgh
cout << str << endl;//321
//
int len = 10;   
str7.resize(len, ch);//把字符串当前大小置为len,并用字符ch填充不足的部分,过多部分截取掉 , 
cout << str << endl; 

2.1.7、查找

//匹配性查找:str字符串在string对象中出现的位置,与str字符串的相关
//find(str,int index.int num)  包括三种重载方法,参数1:表示在string对象中被查找的数据,参数2:表示开始查找的位置,参数3:表示从str中获取它的前num个字符串查找。
//rfind(str,int index.int num),参数含义与上相同,只是是从字符串后面开始查找

string str="123456789012345678900987654321";
	cout <<"str:" << str << endl;
	int pos;
	pos= str.find('3', 0);
	cout<<"从位置0开始查找字符'3'在str中的位置:"<<pos<< endl;//2

	pos= str.find("456", 0);
	cout << "从位置0开始查找字符串“456”在str中的位置:" << pos << endl;//3

	pos= str.find("4567765", 0, 4);
	cout<< "从位置0开始查找字符串“4567765”前4个字符组成的字符串在str中的位置:"<<pos<< endl;//3


	pos = str.rfind('3', string::npos);
	cout << "从反向开始查找字符'3'在str中的位置:" << pos << endl;//27

	pos = str.rfind("456");
	cout << "反向开始查找字符串“456”在str的位置:" << pos << endl;//13

	pos = str.rfind("45677", string::npos, 4);
	cout << "反向查找字符串“4567”中前3个 字符组成的字符串在str的位置:" << pos << endl;//13


//包含性查找:str中的字符在string对象中出现的位置,与str字符串的无关
//find_first_of(str,int index,int num) ;包括三种重载方法(依据参数个数),参数1:表示在string对象中被查找的数据,参数2:表示开始查找的位置,参数3:表示从str中获取它的前num个字符串查找。
//find_first_not_of,首个不出现的位置
//find_last_of,从后向前
//find_last_not_of,从后向前第一个不存在的位置。
	string str1 = "122333444455555666666";
	cout << "str1:" << str1 << endl;
	pos=str1.find_first_of('4', 0);
	cout << "从位置0开始查找字符‘4’在str1第一次出现的位置:" << pos << endl;//6

	pos= str1.find_first_of("45", 0);
	cout << "从位置0开始查找字符串”45“中的字符在str1中第一次出现的位置:" << pos << endl;//6

	pos= str1.find_first_of("7536", 6, 3);
	cout<<"从位置6开始查找字符串”7536“的前3个字符在str1中第一次出现的位置:"<<pos<< endl;//10

	pos=str1.find_first_not_of('4', 8);
	cout << "从str1中4号位置位置查找'4'在不出现的位置:" << pos << endl;//10

	pos= str1.find_first_not_of("34", 2);
	cout << "从str1中第2个位置开始位置查找'34'字符中不存在‘3’、‘4’字符不出现的位置:" << pos << endl;//2

	pos= str1.find_first_not_of("345",6,2);
	cout << "从str1中第6个位置开始位置查找'345'字符串中的前俩个字符不出现的位置:"<<pos<< endl;//2
	
	//下面的last的格式和first的一致,只是它从后面检索!   
	pos= str1.find_last_of('4', string::npos);
	cout << "从str1中反向查找第一个'4'字符出现的位置:" << pos << endl;//9
	pos = str1.find_last_of("45",string::npos );
	cout << "从str1中反向查找'45'字符串中的字符在str1中首次出现的位置:" << pos << endl;//14
	pos = str1.find_last_of("456", string::npos, 2);
	cout << "从str1中反向查找'456'字符串前俩个字符在str1中首次出现的位置:" << pos << endl;//14
	pos = str1.find_last_not_of('6', string::npos);
	cout << "从str1中反向查找'6'字符不出现的位置:" << pos << endl;//14

2.1.8、替换

string str = "12345678901234567890";
str.replace(0, 3, "abc");//删除从0开始的3个字符,然后在0处插入字符串
cout << str << endl;
str.replace(0, 3, "abcdef", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符   
cout<< str<< endl;
str.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符   
cout<< str<< endl;
str.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c   
cout<< str<< endl;

2.1.9、插入

	string str= "abcdefg";
	str.insert(2, "mnop");//在字符串的2位置开始处,插入字符串“mnop”   
	cout<< str<< endl;
	str.insert(2, 2, 'm');//在字符串的0位置开始处,插入2个字符m   
	cout<<str<< endl;
	str.insert(2, "uvwxy", 3);//在字符串的0位置开始处,插入字符串“uvwxy”中的前3个字符   
	cout<< str<< endl;
	str.insert(2, "uvwxy", 1, 2);//在字符串的0位置开始处,插入从字符串“uvwxy”的1位置开始的2个字符   
	cout<<str<< endl;

2.1.10、删除

string str= "gfedcba";
string::iterator it;
it= str.begin();
it++;
str.erase(it);//删除it指向的字符,返回删除后迭代器的位置   
cout<< str<< endl;
str.erase(it, it + 3);//删除it和it+3之间的所有字符,返回删除后迭代器的位置
cout<< str<< endl;
str.erase(2);//删除从字符串位置3以后的所有字符,返回位置3前面的字符   
cout<< str<< endl;


//string s(beg,end) //以区间beg;end(不包含end)内的字符作为字符串s的初值

1、查找摸个字符串是否存在:

str.find("PASS")//返回PASS在字符串的起始位置,不存在则返回-1

2、截取字符串

str.substr(start, temp.length() - start)//参数1:截取的起始位置,参数2:截取的字符串的个数

5、其他

直接获取输入数据

string str1;  
cin >> str1;//当用cin>>进行字符串的输入的时候,遇到空格的地方就停止字符串的读取输入   
cout << str1 << endl;  
cin.get();//这个的作用就是读取cin>>输入的结束符,不用对getline的输入产生影响!   
getline(cin, str1);//字符串的行输入  
cout << str1 << endl;  

3、CString类

3.1简介

关于CSting类使用注意事项:

3.1.1、在非dll或者lib的工程里,使用CString是很容易的,只要两步:

对于没有包含<Windows.h>的stdafx.h中,只要包含了afx.h即可,而对于已经包含了<windows.h>的stdafx.h, 一定需要保证afx.h在windows.h之前被包含。另外由于默认的控制台程序采用的单线程运行库,我们要把它改成多线程库,这些工作只要在stdafx.h中进行修改就可以了,我使用的一个stdafx.h的例子如下(这是一个从向导生成的win32位GUI的程序的stdafx.h修改的)

3.1.2、在dll或者lib的工程中,由于afx.h默认带了一个DllMain,致使要使用CString类需要几个步骤。

1、首先和控制台程序一样,如果编译环境设置了采用单线程库, 要改成多线程库,这个可以从工程属性里进行修改,下面给出的是我常用的方式,可以直接把它复制到工程里使用:

#ifdef _DEBUG
   #pragma comment(lib, "libcmtd.lib")
   #else
   #pragma comment(lib, "libcmt.lib")
   #endif

2、工程目录下创建一个DLLMODUL.CPP文件,并且把它加入到当前工程中。

3、打开DLLMODUL.CPP文件,编辑这个文件为这样:

#include "stdafx.h"
   #ifdef _DEBUG
   #undef THIS_FILE
   static char THIS_FILE[] = __FILE__;
   #endif

   #define new DEBUG_NEW

   /////////////////////////////////////////////////////////////////////////////
   // global data

   // The following symbol used to force inclusion of this module for _USRDLL
   #ifdef _X86_
   extern "C" { int _afxForceUSRDLL; }
   #else
   extern "C" { int __afxForceUSRDLL; }
   #endif

 4、打开stdafx.h,把afx.h包含在windows.h前面。现在可以正常的使用CString了。

3.2、常用函数

3.2.1、CString类对象的初始化(定义)

CString str;//直接声明

//及字符串、指针、字符串数组可以直接转换成CString类。

CString str1(_T("abc"));//运用CString的构造函数,构造函数的参数可以是字符传、指针、字符串数组

CString str2 = _T("defg");//运用“=”号,右边可以字符传、指针、字符数组(未以‘\0’结尾的数据初始化会出现异常);

3.2.2、获取CString类数据长度

csStr="AB中文12";
int len=csStr.GetLength()//UNICODE下为6,ANSI下为8

3.2.3、字符(串)的位置查找方法

1、寻找字符或字符串的在CString中的位置 

Find(Param1,Param2)

Param1参数类型:可以为字符、字符串、指针、数组名、CSting类。作用:被查找的数据。
Param2参数类型:整型。作用:开始查找的位置,位置下标以1开始算
返回值:int类型,被查找数据的起始位置,未找到则返回-1;

CString cstr="abcd1234567";

cstr.Find(Param1,Param2);
//注意:参数1使用字符串数组名,必须确定字符串数组是否是以'\0'结尾。
//因为字符数组未初始化位置以补'\0',对字符数组的使用,注意越界问题,及未以‘\0’结尾
char ch[3] = { '1', '2' };
cstr.Find(ch);//此处返回值为4
char ch1[2] = { '1', '2' };
cstr.Find(ch1);//此处返回值为-1

2、寻找字符串中任意一个字符在CString中的位置

FindOneOf(param)  //param类型可以为:char*、字符数组型、CString类

返回值:找到则返回第一被找到字符在CString中的位置,未找到返回-1;

CString cstr="abcd123456789";
CString cstr3 = "27";
char *ptr = NULL;
ptr = "c2";
char ch[3] = { '1', 'd' };
cout<<cstr.FindOneOf(ptr)<<endl;//返回值为2
cout << cstr.FindOneOf(ch) << endl;//返回值为3
cout << cstr.FindOneOf(cstr3) << endl;//返回值5

3、从后向前查找返回字符在CString中的位置

ReverseFind(param) //param类型为字符

CString cstr="ab8d123456789";
cout << cstr.ReverseFind('8') << endl;//输出值为11

4、寻找字符串中任意一个字符在CString的位置,并且返回在CString此位置之前的子串

 SpanExcluding(param)  //param参数类型:字符串、指针、字符数组、CString类

返回值类型:CString

CString cstr="abcd123456789";
CString cstr3 = "27";
char *ptr = NULL;
ptr = "c2";
char ch[3] = { '1', 'd' };
cout << cstr.SpanExcluding(ptr) << endl;//ab
cout << cstr.SpanExcluding(ch) << endl;//abc
cout << cstr.SpanExcluding(cstr3) << endl;//abcd1

5、查找字符串中任意字符不匹配的字符,并返回CString中不匹配字符之前的字串

SpanIncluding( param ) //param参数类型:字符串、指针、字符数组、CString类

返回类型:CString类

//用于判断某个字符串是否按要求存在的

//此处分析CString是否全由数字组成
CString cstr="11abcd123456789";
CString cstr4 = "1234332890123456789";
CString cstr3 = "1234567890";
cout << cstr.SpanIncluding(cstr3) << endl;//11
cout << cstr4.SpanIncluding(cstr3) << endl;//1234332890123456789

3.2.4、CString类下标操作

1、获取某个位置的字符

 GetAt( param)  //参数类型为整型

返回值为char型

cstr="abcdef";
cout<<cstr.GetAt(2);             //c

2、对某个位置的下标重新赋值 

SetAt( index, ch );//index:需要被改变的位置,ch:被改变后的字符

cstr="abcdef";
cstr.SetAt(2,'x');
cout<<cstr;                      //abxdef

3.2.5、对CString的截取

1、从左截取nCount个字符

 Left( nCount ) //nCount为整型,表示截取的个数

返回类型:CString

cstr="abcdef";
 cout<<cstr.Left(3);          //abc

 2、从右截取nCount个字符

 Right( nCount )//nCount为整型,表示截取的个数

返回类型:CString

cstr="abcdef";
cout<<cstr.Right(3);           //def

3、从某个位置开始截取

Mid( index)  //从某个位置开始截取到末尾

Mid( index,  nCount ) ////从某个位置开始向后截取nCout个字符

cstr="abcdef";
cout<<cstr.Mid(2);           //cdef
cstr="abcdef";
 cout<<cstr.Mid(2,3);         //cde

3.2.6、将数据按格式化输入到CString中(可用于int转CString)

cstr.Format("abc%d",13);
cout<<cstr;                       //abc13

3.2.7、CString的清空和判断是否为空

cstr="abcdef";
cstr.Empty();//对CString进行清空

csStr.IsEmpty();//为空时返回true,不为空时返回为false

3.2.8、对CString进行颠倒、大小写转换操作

CString cstr="ABCdeF123456";
cstr.MakeReverse();//翻转
cout<<cstr;                  //654321FedCBA

cstr.MakeUpper();//小写转大写
cout<<cstr;  //654321FEDCBA

cstr.MakeLower();//大写转小写
cout<<cstr;  // 654321fedcba

3.2.9、比较操作

 1、Compare(param)//区分大小写比较,param参数可以为指针或者字符数组

//相等时返回0,参数值大于CString,返回1,小于时返回-1;比较时是按字符单个比较,如果参数字符只是简单比CString多一个字符,依旧返回-1
CString cstr="ABCDefg";
char *ptr = NULL;
ptr = "ABCDefg";
char ch[9] = { 'A', 'B', 'C', 'D', 'e', 'f', 'G' };
cout << cstr.Compare(ptr) << endl;//0
cout << cstr.Compare(ch) << endl;//1

2、 CompareNoCase //不区分大小写比较,param参数可以为指针或者字符数组

//相等时返回0,参数值大于CString,返回1,小于时返回-1;比较时是按字符单个比较,如果参数字符只是简单比CString多一个字符,依旧返回-1
CString cstr="ABCDefg";
char *ptr = NULL;
ptr = "ABCDefg";
char ch[9] = { 'A', 'B', 'C', 'D', 'e', 'f', 'G' };
cout << cstr.Compare(ptr) << endl;//0
cout << cstr.Compare(ch) << endl;//0

3.2.10、CString类的删除、插入、替换操作

1、删除

从index位置删除nCount个字符

Delete( int index, int nCount = 1 )

cstr="ABCDEF";
cstr.Delete(2,3);
cout<<cstr;              // ABF

 注:下面的方法,碰到第一个不匹配,就不会发生删除,不受数据的顺序影响。

从左开始删除与chTarget或lpszTargets中匹配的字符,一直删到第一个不匹配的字符为止

void TrimLeft( );
 void TrimLeft( TCHAR chTarget );
 void TrimLeft( LPCTSTR lpszTargets );

cstr="aaabaacdef";
cstr.TrimLeft('a');
cout<<csStr;                //baacdef
cstr="aaabaacdef";
cstr.TrimLeft("ab");
cout<<cstr;                //cdef

从右开始删除与chTarget或lpszTargets中匹配的字符,一直删到第一个不匹配的字符为止

void TrimRight( );
void TrimRight( TCHAR chTarget );
void TrimRight( LPCTSTR lpszTargets );

cstr="abcdeaafaaa";
cstr.TrimRight('a');
cout<<csStr;               //abcdeaaf
csStr="abcdeaafaaa";
cstr.TrimRight("fa");
cout<<cstr;                //abcde

从左右开始删除与chTarget或lpszTargets中匹配的字符,一直删到第一个不匹配的字符为止

 void Trim( );
 void Trim( TCHAR chTarget );
 void Trim( LPCTSTR lpszTargets );

 2、从某个位置插入字符或者字符串

 int Insert( int nIndex, TCHAR ch )
int Insert( int nIndex, LPCTSTR pstr )

注意://当index为负数时,插入在对象开头
       //当index超出对象末尾时,插入在对象末尾

cstr="abc";
cstr.Insert(2,'x');
cout<<cstr;                    //abxc
cstr="abc";
csStr.Insert(2,"xyz");
cout<<csStr;                    //abxyzc

 3、移除对象内的指定字符,返回移除的数目

int Remove( TCHAR ch );

cstr="aabbaacc";
cout<<cstr.Remove('a')<<enld;  4              
cout<<cstr;//bbcc

 4、替换字串

 int Replace( TCHAR chOld, TCHAR chNew );
 int Replace( LPCTSTR lpszOld, LPCTSTR lpszNew );

cstr="abcdef";
cout<<cstr.Replace('a','x')<<endl;
cout<<cstr<<endl;                    //xbcdef
cstr="abcdef";
cstr.Replace("abc","xyz");
cout<<csStr<<endl;                    //xyzdef

3.2.11、其他

1、GetBuffer( int nMinBufLength );

注:可以将CString类型转换成char*

获取内部字符缓冲区.返回LPTSTR类型指针。参数nMinBufLength指定缓冲区大小,参数为空则为原内部缓冲区大小。

2、ReleaseBuffer( int nNewLength = -1 );

使用GetBuffer后必须使用ReleaseBuffer以更新对象内部数据,CString对象的任 何方法都应在ReleaseBuffer之后调用

3、LPTSTR GetBufferSetLength( int nNewLength );

获取内部字符缓冲区.返回LPTSTR类型指针。

4、类型转换

4.1、char*转其他类型

char*转string、CString,QString类型直接作为其对象的构造参数即可

char*转int:atoi(s);

char*转float:atof(s);

char*转long:atol(s);

4.2、int类型转其他类型

int转string:

#include <sstream>
stringstream stream;
stream << i;
string name = "name";
name.append(stream.str());

string转char*:为str.c_str()

CString转char:sct.GetBuffer()

 

参考文献:

https://www.cnblogs.com/milanleon/p/5623074.html

https://blog.csdn.net/fanhansheng/article/details/52636379

猜你喜欢

转载自blog.csdn.net/w_x_myself/article/details/82687102