【C++中的string字符串常用函数】tolower(), toupper(), isalnum(), isdigit(), islower(), isupper(), isalpha()

本文介绍了如何在C++中使用`substr`函数对字符串进行操作,如提取子串,以Hello,World!为例展示了从不同位置和长度提取字符串的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • tolower()/toupper():将字符串string转换大小写
  • isalnum():判断字符串是否为字母+十进制数字
  • isdigit():判断字符串是否为十进制数字
  • islower(): 判断字符串是否为小写
  • isupper(): 判断字符串是否为大写
  • isalpha(): 判断字符串是否为字母
  • substr(pos, len):提取字符串从pos开始的长度为len的子串
#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    
    // 提取从索引位置5开始的子字符串,并将其存储到newStr中
    std::string newStr = str.substr(5);
    std::cout << "New string: " << newStr << std::endl;
    
    // 提取从索引位置6开始,长度为6的子字符串,并将其存储到newStr2中
    std::string newStr2 = str.substr(6, 6);
    std::cout << "New string 2: " << newStr2 << std::endl;
    
    return 0;
}
New string: , World!
New string 2: World!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值