C++三种常用的字符串总结

C++三种常用的字符串表示方法——string类
转载自https://www.cnblogs.com/fishsorry/p/3608822.html

//1.string类重载运算符operator>>用于输入,同样重载运算符operator<<用于输出操作
string str1;
getline(cin, str1);//字符串的行输入
cout << str1 << endl; 
cout<<endl;

//2.string类的构造函数 
char *s = "bbbbb";
string str2(s);//用c字符串s初始化 
cout << str2 << endl;

char ch = 'c';
string str3(5, ch);//用n个字符ch初始化 
cout << str3 << endl; 
cout<<endl;

//3.string类的字符操作
string str4 = "abcde";
ch = str4.at(4);//at()返回当前字符串中第n个字符的位置,并且提供范围检查,当越界时会抛出异常!  
cout << ch << endl; 
cout<<endl;

//4.string的特性描述
string str5 = "abcdefgh";
int size;
size = str5.capacity();//返回当前容量,一般在x86上都是输出15
cout << size << endl; 
size = str5.max_size();//返回string对象中可存放的最大字符串的长度 ,一般在x86上都是输出-2
cout << size << endl; 
size = str5.size();//返回当前字符串的大小 
cout << size << endl; 
size = str5.length();//返回当前字符串的长度 
cout << size << endl; 
int len = 10; 
str5.resize(len, ch);//把字符串当前大小置为len,并用字符ch填充不足的部分 
cout << str5 << endl; 
cout<<endl;

//5.string的赋值
string str6;
str6.assign(str5);//把字符串str5赋给当前字符串 
cout << str6 << endl; 
str6.assign(s);//用c类型字符串s赋值 
cout << str6 << endl; 
str6.assign(s, 2);//用c类型字符串s开始的n个字符赋值 
cout << str6 << endl; 
str6.assign(len, ch);//用len个字符ch赋值给当前字符串 
cout << str6 << endl; 
str6.assign(str5, 0, 3);//把字符串str7中从0开始的3个字符赋给当前字符串 
cout << str6 << endl; 
cout<<endl;

//6.string的连接,用append
string com;
com += str6;//把字符串str9连接到当前字符串的结尾 
cout << com << endl;
com.append(s);//把c类型字符串s连接到当前字符串的结尾 
cout << com << endl; 
com.append(s, 2);//把c类型字符串s的前2个字符连接到当前字符串的结尾 
cout << com << endl; 
com.push_back('k');//把一个字符连接到当前字符串的结尾 
cout << com << endl; 
cout<<endl;

//7.string的比较 ,用compare
//以下的3个函数同样适用于c类型的字符串,在compare函数中>时返回1,<时返回-1,=时返回0
int flag;
flag = com.compare(str6);//比较两个字符串的大小,通过ASCII的相减得出! 
cout << flag << endl; 
flag = com.compare(6, 12, str6);//比较com字符串从6开始的12个字符组成的字符串与str6的大小 
cout << flag << endl;
flag = com.compare(6, 12, str6, 3, 5);//比较com字符串从6开始的12个字符组成的字符串与str6字符串从3开始的5个字符组成的字符串的大小 
cout << flag << endl; 
cout<<endl;

//8.string的子串
string str7;
str7= com.substr(10, 15);//返回从下标10开始的15个字符组成的字符串 
cout << str7 << endl; 
cout<<endl;

//9.string的交换
str7.swap(com);//交换str7与com的值 
cout << str7 << endl; 
cout<<endl;

//10.string的查找,查找成功时返回所在位置,失败时返回string::npos的值,即是-1 
string str8 = "abcdefghijklmnopqrstuvwxyz";
int pos;
pos = str8.find('i', 0);//从位置0开始查找字符i在当前字符串的位置 
cout << pos << endl;
pos = str8.find("ghijk", 0);//从位置0开始查找字符串“ghijk”在当前字符串的位置 
cout << pos << endl; 
pos = str8.find("opqrstuvw", 0, 4);//从位置0开始查找字符串“opqrstuvw”前4个字符组成的字符串在当前字符串中的位置 
cout << pos << endl; 
pos = str8.rfind('s', string::npos);//从字符串str8反向开始查找字符s在字符串中的位置 
cout << pos << endl; 
pos = str8.rfind("klmn", string::npos);//从字符串str8反向开始查找字符串“klmn”在字符串中的位置 
cout << pos << endl;
pos = str8.rfind("opqrstuvw", string::npos, 3);//从string::pos开始从后向前查找字符串s中前n个字符组成的字符串在当前串中的位置 
cout << pos << endl; 
cout<<endl;

string str9 = "aaabbbcccddeeffgghhiijjkkllmmandjfaklsdfpopdtwptioczx";
pos = str9.find_first_of('d', 0);//从位置0开始查找字符d在当前字符串第一次出现的位置 
cout << pos << endl; 
pos = str9.find_first_of("eefff", 0);//从位置0开始查找字符串“eeefff“在当前字符串中第一次出现的位置 
cout << pos << endl; 
pos = str9.find_first_of("efff", 0, 3);//从位置0开始查找当前串中第一个在字符串”efff“的前3个字符组成的数组里的字符的位置 
cout << pos << endl;
pos = str9.find_first_not_of('b', 0);//从当前串中查找第一个不在串s中的字符出现的位置 
cout << pos << endl; 
pos = str9.find_first_not_of("abcdefghij", 0);//从当前串中查找第一个不在串s中的字符出现的位置 
cout << pos << endl; 
pos = str9.find_first_not_of("abcdefghij", 0, 3);//从当前串中查找第一个不在由字符串”abcdefghij”的前3个字符所组成的字符串中的字符出现的位置 
cout << pos << endl; 
//下面的last的格式和first的一致,只是它从后面检索! 
pos = str9.find_last_of('b', string::npos);
cout << pos << endl;
pos = str9.find_last_of("abcdef", string::npos);
cout << pos << endl;
pos = str9.find_last_of("abcdef", string::npos, 2);
cout << pos << endl; 
pos = str9.find_last_not_of('a', string::npos);
cout << pos << endl; 
pos = str9.find_last_not_of("abcdef", string::npos);
cout << pos << endl;
pos = str9.find_last_not_of("abcdef", string::npos, 3);
cout << pos << endl;
cout<<endl;

//11.string的替换,用replace
string str10 = "abcdefghijklmn";
str10.replace(0, 3, "qqqq");//删除从0开始的3个字符,然后在0处插入字符串“qqqq” 
cout << str10 << endl; 
str10.replace(0, 3, "vvvv", 2);//删除从0开始的3个字符,然后在0处插入字符串“vvvv”的前2个字符 
cout << str10 << endl; 
str10.replace(0, 3, "opqrstuvw", 2, 4);//删除从0开始的3个字符,然后在0处插入字符串“opqrstuvw”从位置2开始的4个字符 
cout << str10 << endl; 
str10.replace(0, 3, 8, 'c');//删除从0开始的3个字符,然后在0处插入8个字符 c 
cout << str10 << endl; 
cout<<endl;


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

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

****//14.字符串的流处理***
string str13("hello,this is a test");
istringstream is(str13);
string s1,s2,s3,s4;
is>>s1>>s2>>s3>>s4;//s1="hello,this",s2="is",s3="a",s4="test"
ostringstream os;
os<<s1<<s2<<s3<<s4;
cout<<os.str() << endl;

关于c++builder的一些字符串总结
–转载自http://www.cppfans.com/cbknowledge/reference/strings/ansistring.asp#info

由于 AnsiStringT 的存在,让字符编码之间转换变得非常的容易了。
UNICODE (UTF-16) 和 ANSI 之间字符编码转换是自动的
UnicodeString u;
AnsiString s;
s = u; // UNICODE (UTF-16) 转 ANSI
u = s; // ANSI 转 UNICODE (UTF-16)

UTF-8 和 ANSI 之间字符编码转换是自动的
UTF8String u;
AnsiString s;
s = u; // UTF-8 转 ANSI
u = s; // ANSI 转 UTF-8

GBK, GB2312, BIG5 和 ANSI 之间字符编码也可以自动转换
AnsiStringT<936> gbk; // 大陆和新加坡简体中文和繁体中文都有
AnsiStringT<950> big5; // 港澳台的 BIG5 只有繁体中文
AnsiStringT<20936> gb2312; // 大陆的早期编码 GB2312 只有简体中文
AnsiString s; // 以上字符串之间可以随意互相赋值进行自动转码
s = gbk;
gbk = big5;
big5 = CHS2CHT(gb2312); // GB2312 和 BIG5 之间需要简体中文和繁体中文之间转换
……
C++ Builder 函数,整数和字符串之间的转换:
在这里插入图片描述

例:利用 C++ Builder 自带的函数进行 16 进制转 10 进制。把编辑框 Edit1 里面的 16 进制数转为 10 进制,输出到编辑框 Edit2 里面

void __fastcall TForm1::Button1Click(TObject *Sender)
{
try
{
int i = StrToInt(L"0x" + Edit1->Text);
Edit2->Text = IntToStr(i);
}
catch(Exception &e)
{
ShowMessage(L"转换失败,错误信息:\r\n" + e.Message);
}
}
运行结果:

如果 Edit1 里面输入 xyz,点击 Button1 按钮,会弹出错误提示:转换失败,错误信息:0xxyz is not a valid integer value.
如果 Edit1 里面输入 abcd,点击 Button1 按钮,Edit2 里面会输出 43981,即 16 进制的 abcd 转为了 10 进制的 43981

例:把二进制数字符串转为 10 进制数

void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString s = Edit1->Text;
wchar_t *ep;
unsigned long i = wcstoul(s.c_str(), &ep, 2);

if(*ep)
ShowMessage(L"从这里开始无法转换:" + UnicodeString(ep));
else
Edit2->Text = i;
}
运行结果:

如果 Edit1 里面输入 101010,点击按钮 Button1,Edit2 里面输出 42,即二进制数据 101010 转为 10 进制等于 42,
如果 Edit1 里面输入 101023,点击按钮 Button1,会弹出错误提示:从这里开始无法转换:23

一些其他的转换:
1 UpperCase
  将指定的AnsiString字符串转换为大写形式,函数原型如下:
  AnsiString __fastcall UpperCase(const AnsiString S);
  2. LowerCase
  将指定的AnsiString字符串转换为小写形式,函数原型如下:
  AnsiString __fastcall LowerCase(const AnsiString S);
  3. CompareStr
  比较两个AnsiString字符串,函数原型如下:
  int __fastcall CompareStr(const AnsiString S1, const AnsiString S2);
  4. CompareText
  比较两个AnsiString字符串,函数原型如下:
  int __fastcall CompareText(const AnsiString S1, const AnsiString S2);
  5. StrLen
  返回字符串的长度,函数原型如下:
  Cardinal __fastcall StrLen(const char * Str);
  6. StrEnd
  返回字符串结尾指针,函数原型如下:
  char * __fastcall StrEnd(const char * Str);
  7. StrMove
  从源字符串向目的字符串拷贝指定数目的字符,函数原型如下:
  char * __fastcall StrMove(char * Dest, const char * Source, Cardinal Count);
  8. StrCopy
  将源字符串拷贝到目的字符串中,函数原型如下:
  char * __fastcall StrCopy(char * Dest, const char * Source);

猜你喜欢

转载自blog.csdn.net/qq_37809058/article/details/85111676
今日推荐