C++字符串操作总结

3 篇文章 0 订阅

1. 单个字符的处理

需要包含头文件 cctype

函数说明
isalnum( c )c 是字母或数字
isalpha( c )c 是字母
iscntrl( c )c 是控制字符
isdigit( c )c 是数字
isprint( c )c 是可打印字符(空格或可视形式)
ispunct( c )c 是标点符号(c 不是控制字符、数字、字母、可打印空白)
isspace( c )c 是空白(空格、制表符、换行符、回车符、置页符之一)
isgraph( c )c 不是空格但可以打印
islower( c )c 是小写字母
isupper( c )c 是大写字母
tolower( c )如果是大写,输出小写,否则返回本身
toupper( c )如果是小写,输出大写,否则返回本身
isxdigit( c )c 是十六进制数字
#include <string>
#include <cctype> // 处理单个字符的函数库
using namespace std;
int main()
{
		// 1. 处理字符串
		string s;
		string s2;
		for (char a : s) {
			if (isalnum(a)) // 如果是字母或数字
			{
				if (isupper(a)) // 如果是大写字母
				{
					s2.push_back(tolower(a)); // 大写转换成小写
				}
				else
				{
					s2.push_back(a);
				}
			}
		}

		// 反转字符串
		string s3 = s2;
		reverse(s2.begin(), s2.end());

		// 判断是否相等
		if( s2 == s3)

return 0;
}

2. 字符串的处理

需包含头文件string

2.1 int 转 string

int a = 10;
string s = to_string(a);
// s = "10";

2.2 string 转 int

string s = "10";
int num = stoi(s);
// num = 10;
// 另外还有 stof(), stod(), stol()等,分别表示string转float, double,long.

2.3 查找子串

string s = "01234";
if (s.find("01") != string::npos) {
    // do sth
}

2.4 获取子串

int start = 5, n = 4;
string ss = "This is csdn blog!"
string s = ss.substr(start, n); // 从start开始的n个,  s = "is c"
string s = ss.substr(start); // 从 start开始到结尾, s = "is csdn blog!"

2.5 字符串在set中的排序(按字典序)

// 从大到小 
#include <set>
#include <algorithm>
set<string, greater<string>> ss;
// 从小到大 
set<string> ss;
ss.insert("ace");

2.6 字符串输入

// 获取一行 
#include <sstream>
string str;
getline(cin, str); // str = "abc def go"

// 以特殊符号为分割 
string str =  "abc def go";
stringstream ss;
ss << str;
string tmp;
string res;
while (getline(ss, tmp, ' ') {
   res += tmp;
   tmp.clear();
   }
   // res = "abcdefgo"

// 获取以空格为间隔的字符串 
// cin "abc def go"
cin >> str; // str = "abc", 空格后的被截断了

2.7 初始化

string msg{"good"}; // 调用构造函数初始化
string msg.assign(32, '0'); // 使用32个'0'填充字符串

2.8 插入字符

int pos = 4;
string str = "you are beautiful.";
int size = 3;
char a = "*";
str.insert(pos, size, a); // 在pos前面插入size个char,  str = "you ***are beautiful."

2.9 替换

int pos = 4;
string str = "you are beautiful.";
string msg = "he is boy.";
int size = 3;
str.replace(pos, size, msg); // 从pos用msg替换size个字符, str = "you he is boy. beautiful."

2.10 数字(ASCII码)转字符串

// 数字10转十六进制大写A
int num = 10;
string msg += char((num - 10) + 'A'); // 在10进制转16进制用得到, 10 转成A

2.11 进制转化(利用字符串进行 十进制 十六进制 二进制 互转)

// 十进制转16进制
#include <sstream>
string str; // Hexadecimal
int input = 10; // Decimalism
stringstream stream;
stream << std::hex << input; // 十进制转16进制
stream >> str; // 小写字符串格式 str = "0a"
// 把所有的小写字母转成大写字母
transform(str.begin(), str.end(), str.begin(), [](char& a) {return toupper(a);}); // str = "0A"
 
// 十六进制转10进制
string result3 =0A”; // Hexadecimal
int input; // Decimalism
stream << std::hex << result3;  // 8进制 std::oct
stream >> input;  // input= 10

// 十进制转二进制
#include <bitset>
int input = 10; // dec
bitset<8> bit(input);  // bit = {{1} {0} {1} {0}}
string dec = bit.to_string();  // dec = "1010"

// 二进制转十进制
bitset<8> bit("1010");
int dec = bit.to_ulong(); // dec = 10

2.12 位运算

int val = 10; // 二进制是1010
int bit = val & 1; // 与运算 bit = 0 (1010 & 1 = 0),缺的位不能与运算
int bit2 = val >> 1; // 移位操作,右移一位是 除2。 bit2 = 5

2.13 消除字符串中的特殊字符

int main()
{
    string str1 = "Hello world Text  with some   spaces";
    str1.erase(std::remove(str1.begin(), str1.end(), ' '), str1.end());
    // str1 = "HelloworldTextwithsomespaces";
    
    string str2 = "Hello world Text\n  with\t some \t  white spaces\n\n";
    auto erased = remove_if(str2.begin(), str2.end(), [](unsigned char x) {return std::isspace(x);});
    str2.erase(erased, str2.end());
    // str2 = "HelloworldTextwithsomewhitespaces";
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值