c++Csring 截取字符串

这里写图片描述

关于TCHAR, char, wchar_t三种字符类型的区别,会在随后的文章中详细解释!下面是一、CString类的几种基本操作:
1、长度:GetLength();
CString str(_T(“abc”));
int len = str.GetLength(); //len == 3

2、是否为空(即不含字符):IsEmpty();
3、清空字符串:Empty();
CString str(_T(“abc”));
BOOL mEmpty = str.IsEmpty(); //mEmpty == FALSE
str.Empty();
mEmpty = str.IsEmpty(); //mEmpty == TRUE

4、转换大小写:MakeUpper(),MakeLower();
5、转换顺序:MakeReverse();

二、字符串的查找
1、Find:从制定位置开始查找指定字符串,返回其位置(找不到返回-1)
CString str(_T(“abcdefg”));
int idx = str.Find(_T(“cde”), 0); //idx 的值为2;

2、ReverseFind:从字符串末尾开始查找指定的字符,返回其位置,找不到返回 -1,虽然是从后向前查找,但是位置为从开始算起;
CString str(_T(“abcdefg”));
int idx = str.ReverseFind(‘e’); //idx 的值为4;

3、FindOneOf:查找参数中给定字符串中的任意字符,返回第一次出现的位置
CString str(_T(“abcabcd”));
int idx = str.FindOneOf(_T(“cbd”)); //idx 的值为1;

三、字符串的提取
Left,Mid,Right:分别实现从CString对象的左、中、右进行字符串的提取操作
CString str(_T(“abcd”));
CString strResult = str.Left(2); //strResult == ab
strResult = str.Mid(1); //strResult == bcd
strResult = str.Mid(0, 2); //strResult == ab
strResult = str.Right(2); //strResult == cd

四、其他类型与CString类型的转换,CString str;
1、格式化字符串Format:实现从int、long等数值类型、TCHAR、TCHAR*等类型向CString类型转换(注:TCHAR、TCHAR*等类型向CString类型转换,可以直接赋值)
– CString -> int:_ttoi()
– CString -> TCHAR* :
1)TCHAR* T = str.GetBuffer(); str.ReleaseBuffer();
2)TCHAR* T = (LPTSTR)(LPCTSTR)str;

五、CString对象的Ansi与Unicode转换
1、当前工程环境Unicode(窄字节(Ansi)向宽字节(Unicode)转换)
CString str;
str = “abc”;
char* p = “defg”;
str = p;

2、当前工程环境非Unicode(宽字节向窄字节转换)
CString str;
str = L”abc”;
wchar_t* p = L”defg”;
str = p;

六、CString对象包含字符串所占用的字节数
CString str;
int n = str.GetLength() * sizeof(TCHAR);
**错误的求法:sizeof(CString)、sizeof(str),这是求指针长度,总是为4
………………………………………………………………………………………………………………

我的使用:
目的:提取出邮件地址
From: “wushuang@bm.com” wushuang@bm.com
int idx=contents1.ReverseFind(‘<’);
contents1=contents1.Right(contents1.GetLength()-idx);
输出结果:wushuang@bm.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 你可以使用 `substr` 函数来截取 C++ 中的字符串。该函数的语法如下: ```c++ string substr (size_t pos, size_t len) const; ``` 其中,`pos` 是从哪个位置开始截取,`len` 是截取的长度。例如,要截取字符串 `str` 的第 3 个字符开始的 5 个字符,可以这样写: ```c++ string str = "hello world"; string substr = str.substr(2, 5); // 从第3个字符开始,截取5个字符 cout << substr << endl; // 输出:lo wo ``` 需要注意,`substr` 函数返回的是截取后的子字符串,原字符串不会受到影响。 ### 回答2: 在C语言中,可以使用多种方法来截取字符串。其中一种常见的方法是使用函数库中的字符串处理函数来实现。 其中,可以使用"strncpy"函数来截取字符串。该函数的原型为: ``` char* strncpy(char* destination, const char* source, size_t num); ``` 参数说明: - destination:目标字符串的指针,用于存储截取后的结果; - source:源字符串的指针,即需要被截取的字符串; - num:要截取的字符个数。 使用方法如下: ```c #include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[10]; strncpy(destination, source, 5); destination[5] = '\0'; printf("截取后的字符串是:%s\n", destination); return 0; } ``` 以上代码将会输出:"截取后的字符串是:Hello"。 需要注意的是,在截取字符串时,需要留出足够的空间存储截取后的结果,并且手动添加字符串结束符"\0"。 除了使用"strncpy"函数外,还可以使用其他的字符串处理函数,如"strtok"、"strstr"等,根据具体需求选择合适的函数来截取字符串。 ### 回答3: 在C++中,可以使用一些方法来截取字符串。具体步骤如下: 1. 使用C++的字符串类来存储和操作字符串。可以使用`std::string`来代替C语言中的字符数组。 2. 使用`substr()`函数来实现字符串的截取。`substr()`函数有两个参数,第一个参数是截取的开始位置,从0开始计数,第二个参数是截取的长度。 3. 通过指定开始位置和截取长度,可以截取想要的子字符串。例如,如果要截取从位置2开始的长度为5的子字符串,可以使用`substr(2, 5)`来实现。 下面是一个简单的例子来演示如何在C++截取字符串: ```c++ #include <iostream> #include <string> int main() { std::string str = "Hello, World!"; std::string subStr = str.substr(2, 5); std::cout << "截取的子字符串为: " << subStr << std::endl; return 0; } ``` 上述例子中,原始字符串为"Hello, World!",使用`substr()`函数截取的开始位置为2,长度为5的子字符串。输出结果为:"截取的子字符串为: llo, "。 以上就是在C++中使用C字符串截取字符串的基本方法。希望对你有帮助!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值