C++单字符串和宽字符串学习 2021.4.20
- 1、char 和 string
- 1.1 单字符 char
- 1.2 单字符数组 char[] 和 char*
- 1.2.1 char[]
- 1.2.2 char*
- 1.3 单字符串 string
- 1.4 char[] 转 string
- 1.5 cha[] 转char*
- 1.6 string 转 char*
- 1.7 string 转 char[]
- 2、wchar_t 和 wstring
- 2.1 宽字符 wchar_t
- 2.2 宽字符数组 wchar_t[] 和 wchar_t*
- 2.2.1 wchar_t[]
- 2.2.2 wchar_t*
- 2.3 宽字符串 wstring
- 2.4 wchar_t[] 转 wstring
- 2.5 wchar_t[] 转 wchar_t*
- 2.6 wstring 转 wchar_t[]
- 2.7 wstring 转 wchar_t*
- 3、string与wstring互相转换
- 3.1 string 转 wstring
- 3.2 wstring 转 string
- 3.3 wchar_t * 转char *
- 3.4 wchar_t 转char *
1、char 和 string
1.1 单字符 char
单字符通常都是只占用一个字节存储的字符,包括数字、大小写英文字母和一些常用标点符号等,其中’\n’为换行符,’\0’为字符串结束符,其对应的ASCII吗值为0,’\r’为回车符,’\t’为制表符。
#include <iostream>
using namespace std;
int main()
{char char_a = '1';int number_a = char_a - '0';//可通过-'0'判断是否为数字字符while (true){cout << "请输入单个字符:";cin >> char_a;cout << "您输入的字符为: " << char_a << endl;if (char_a - '0' >= 0 && char_a - '0' <= 9)cout << char_a << " 为数字字符!" << endl;else if (char_a >= 65 && char_a <= 90)cout << char_a << " 为大写英文字母!" << endl;else if (char_a >= 97 && char_a <= 122)cout << char_a << " 为小写英文字母!" << endl;}system("pause");return 0;
}
1.2 单字符数组 char[] 和 char*
多个单字符就可以构成一定长度的字符串,而字符串可以用字符数组char[]来存储,char * 也可以来存储字符串数组,但通常char * 用来遍历char[]中的所有字符。
1.2.1 char[]
#include <iostream>
using namespace std;
int main()
{char a[] = {'a','b','1','5','P','\0'};//'\0'为字符串结束符,可通过它来获取字符串的长度int length = 0;while (a[length] != '\0')length++;cout << "字符串\"" << a << "\"的长度为" << length << endl;cout << "请输入" << length << "个字符:";for (int i = 0; i < length; i++)cin >> a[i];cout << "修改后的字符串为:" << a << endl;char str[12] = {"hello world"};system("pause");return 0;
}
1.2.2 char*
#include <iostream>
using namespace std;
int main()
{char str[12] = {"hello world"};//char str[] = { "hello world" };//不设置长度也可以char *a = str;//通过常量字符串来初始化int length = 0;while (a[length] != '\0')length++;cout << "字符串\"" << a << "\"的长度为" << length << endl;cout << "请输入" << length << "个字符:";for (int i = 0; i < length; i++)cin >> a[i];cout << "修改后的字符串为:";for (int i = 0; i < length; i++)cout << a[i];cout << endl;system("pause");return 0;
}
1.3 单字符串 string
#include <iostream>
#include <string>
using namespace std;
int main()
{string str1 = "ABCEdrt124abR";cout << str1 << endl;int length = 0;while (str1[length] != '\0')length++;cout << "字符串\"" << str1 << "\"的长度为" << length << endl;cout << "请输入" << length << "个字符:";for (int i = 0; i < length; i++)cin >> str1[i];cout << "修改后的字符串为:" <<str1<< endl;string str2 = "9GtR56Erlbsgfs";string str = str1 + str2;//字符串连接成一个字符串cout << "连接后的字符串为:" << str << endl;system("pause");return 0;
}
1.4 char[] 转 string
string char_arrayTostring(char a[])//字符数组char[]转string函数
{string s = "";int j = 0;while (a[j] != '\0')s += a[j++];return s;
}
1.5 cha[] 转char*
char str[] = { "Hello My Computer" };//不设置长度也可以
char *a = str;//通过常量字符串来初始化
int length = 0;
while (a[length] != '\0')length++;
cout << "字符串\"" << a << "\"的长度为" << length << endl;
for (int i = 0; i < length; i++)cout << a[i];
cout << endl;
1.6 string 转 char*
string str_string = "AFDSDSFS8345otsdgf";
char *c;
c = &str_string[0];
while (*c != '\0')
{cout << *c;c++;
}
cout << endl;
1.7 string 转 char[]
string str = "ABCDEFG";
char a[20];
strcpy_s(a, str.c_str());//用到 c_str()函数
int length = 0;
while (a[length] != '\0')length++;
cout << "字符串\"" << a << "\"的长度为" << length << endl;
for (int i = 0; i < length; i++)cout << a[i];
cout << endl;
2、wchar_t 和 wstring
2.1 宽字符 wchar_t
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t han = L'汉', zi = L'字';//以单个汉字的方式给unicode中文字符赋值cout << han << " " << zi << " " << endl;//输出中文字符对应的十进制数wcout << han << " " << zi << " " << endl;//输出中文字符wchar_t mychar1 = 055527;//以八进制数字给unicode中文字符赋值cout << mychar1 << " ";wcout << mychar1 << endl;mychar1 = 23383;//以十进制数字给unicode中文字符赋值cout << mychar1 << " ";wcout << mychar1 << endl;mychar1 = 0x5b57;//以十六进制数字给unicode中文字符赋值cout << mychar1 << " ";wcout << mychar1 << endl;wcout << mychar1;cout << "对应的十六进制、十进制和八进制数分别为:" << showbase << hex << mychar1 << " " << showbase << dec << mychar1 << " " << showbase << oct << mychar1 << endl;wchar_t a = L'中';wcout << a<<'\n';cout << "请输入一个宽字符的汉字:";wcin >> a;cout << "您输入的宽字符汉字:";wcout << a;cout <<"对应的十六进制、十进制和八进制数分别为:" << showbase << hex << a << " " << showbase << dec << a << " " << showbase << oct << a << endl;system("pause");return 0;
}
2.2 宽字符数组 wchar_t[] 和 wchar_t*
2.2.1 wchar_t[]
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t test[] = L"中国梦必胜";//wchar_t test[] = { L'我',L'是',L'中',L'国',L'人',L'\0' };//第二种初始化方式cout << test << " " << endl;wcout << test << " " << endl;int len = 0;while (test[len] != L'\0')len++;for (int i = 0; i < len; i++){cout << test[i] << " ";wcout << test[i] << endl;}cout <<"长度为:"<< len << endl;cout << "系统自带函数统计长度为:" << wcslen(test) << endl;cout << "请重新输入" << len << "个汉字修改上面的字符串:";for (int i = 0; i < len; i++)wcin >> test[i];cout << "修改后的字符串:";wcout << test << endl;//第三种初始化方式cout << "请输入8个汉字:";wchar_t s[]=L" ";int i = 0;for (;i < 8; i++){wcin >> s[i];}wcout << s;cout << endl;system("pause");return 0;
}
2.2.2 wchar_t*
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t test1[] = L"中国梦必胜";wchar_t* test = test1;cout << test << " " << endl;wcout << test << " " << endl;int len = 0;while (test[len] != L'\0')len++;for (int i = 0; i < len; i++){cout << test[i] << " ";wcout << test[i] << endl;}cout <<"长度为:"<< len << endl;cout << "系统自带函数统计长度为:" << wcslen(test) << endl;cout << "请重新输入" << len << "个汉字修改上面的字符串:";for (int i = 0; i < len; i++)wcin >> test[i];cout << "修改后的字符串:";wcout << test<< endl;system("pause");return 0;
}
2.3 宽字符串 wstring
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wstring wst = { L'我',L'是',L'中',L'国',L'人',L'\0' };int len = 0;while (wst[len] != L'\0')len++;wcout << wst;cout <<"长度为:"<< len << endl;cout << "请重新输入" << len << "个汉字修改上面的字符串:";for (int i = 0; i < len; i++)wcin >> wst[i];cout << "修改后的字符串:";wcout << wst << endl;wchar_t ch1[] = L"中国人";//宽字符数组std::wstring wstr(ch1);wcout << wstr << endl;system("pause");return 0;
}
2.4 wchar_t[] 转 wstring
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;wstring wchart_arrayTowstring(wchar_t a[])
{wstring wstr=L"";int j = 0;while (a[j] != L'\0'){wstr += a[j];j++;}return wstr;
}
int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t test1[] = L"中国推动世界和平";wstring wstr1 = wchart_arrayTowstring(test1);wcout << wstr1<< endl;int len = 0;while (wstr1[len] != '\0')len++;cout << "请输入" << len << "个汉字来修改字符串:";for (int i = 0; i < len; i++)wcin >> wstr1[i];wcout << wstr1 << " " << endl;system("pause");return 0;
}
2.5 wchar_t[] 转 wchar_t*
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t test1[] = L"中国推动世界和平";wchar_t* test = test1;wcout << test<< endl;int len = 0;while (test[len] != '\0')len++;cout << "请输入" << len << "个汉字来修改字符串:";for (int i = 0; i < len; i++)wcin >> test[i];wcout << test << " " << endl;system("pause");return 0;
}
2.6 wstring 转 wchar_t[]
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wstring str = L"有一个强大的祖国在身后";wchar_t a[20];int length = 0;while (str[length] != '\0')length++;cout << "字符串\"";wcout << str;cout<< "\"的长度为" << length << endl;for (int i = 0; i < length; i++)a[i] = str[i];wcout << a;cout << endl;system("pause");return 0;
}
2.7 wstring 转 wchar_t*
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wstring str = L"有一个强大的祖国在身后";wchar_t *a= &str[0];int length = 0;while (a[length] != '\0')length++;cout << "字符串\"";wcout << a;cout<< "\"的长度为" << length << endl;system("pause");return 0;
}
3、string与wstring互相转换
3.1 string 转 wstring
MultiByteToWideChar()该函数映射一个字符串到一个宽字符(unicode)的字符串,将单字符组成的多字节string转换为宽字符串wstring,则需要利用系统自带的MultiByteToWideChar()函数进行转换。
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;wstring stringTowstring(string str)
{int len = MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, NULL, 0);if (len == 0)return wstring(L"");wchar_t* wct = new wchar_t[len];if (!wct)return std::wstring(L"");MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wct, len);wstring wstr(wct);delete[] wct;wct = NULL;return wstr;
}
int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式string s = "有一个强大的祖国在身后";wstring a = stringTowstring(s);int length = 0;while (a[length] != L'\0')length++;cout << "字符串\"";wcout << a;cout<< "\"的长度为" << length << endl;system("pause");return 0;
}
3.2 wstring 转 string
WideCharToMultiByte()该函数可以映射一个unicode字符串到一个多字节字符串,执行转换的代码页、接收转换字符串、允许额外的控制等操作。将宽字符串转换多个单字符组成的字符串,则需要利用WideCharToMultiByte()函数进行转换。
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;string wstringTostring(wstring wstr)
{int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, NULL, 0, NULL, NULL);if (len == 0)return string("");char* psz = new char[len];if (!psz)return string("");WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, psz, len, NULL, NULL);string str(psz);delete[] psz;psz = NULL;return str;
}int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wstring s = L"有一个强大的祖国在身后";string a = wstringTostring(s);int length = 0;while (a[length] != '\0')length++;cout << "字符串\""<< a << "\"的长度为" << length << endl;system("pause");return 0;
}
3.3 wchar_t * 转char *
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;char* pwchartTopchar(wchar_t* pwcstr)
{int size = WideCharToMultiByte(CP_OEMCP, 0, pwcstr, wcslen(pwcstr), NULL, 0, NULL, NULL); //第一次调用确认转换后单字节字符串的长度,用于开辟空间char* pcstr = new char[size + 1];WideCharToMultiByte(CP_OEMCP, 0, pwcstr, wcslen(pwcstr), pcstr, size, NULL, NULL); //第二次调用将双字节字符串转换成单字节字符串pcstr[size] = '\0';return pcstr; //string pKey = pCStrKey;可以转换为string字符串
}
int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t *wcstr = L"我们都有一个家";char *a = pwchartTopchar(wcstr);int length = 0;while (a[length] != '\0')length++;cout << "字符串\""<< a << "\"的长度为" << length << endl;system("pause");return 0;
}
3.4 wchar_t 转char *
#include <iostream>
#include<string>
#include <windows.h> //这个头文件必须添加
#include <iomanip>
#include <fstream>
using namespace std;char* wcharTopchar(wchar_t wchar)
{wchar_t* pwchar = &wchar;int size = WideCharToMultiByte(CP_OEMCP, 0, pwchar, wcslen(pwchar), NULL, 0, NULL, NULL);char* pchar = new char[size + 1];WideCharToMultiByte(CP_OEMCP, 0, pwchar, wcslen(pwchar), pchar, size, NULL, NULL);pchar[size] = '\0';return pchar;
}
int main()
{wcin.imbue(std::locale("chs"));//载入中文字符输入方式wcout.imbue(std::locale("chs"));//载入中文字符输入方式wchar_t wcstr = L'家';char *a = wcharTopchar(wcstr);int length = 0;while (a[length] != '\0')length++;cout << "字符串\""<< a << "\"的长度为" << length << endl;system("pause");return 0;
}