C++常用字符串函数大全(2)
本文为C++字符串函数系列文章的第2篇,主要介绍C++标准库中常用的字符串函数,包括:
strncpy()
: 复制n个字符到目标字符串中。strncat()
: 将目标字符串和n个字符的源字符串拼接到一起。strstr()
: 在字符串中查找子串。strspn()
: 返回目标字符串开头连续包含源字符串字符的数目。strcspn()
: 返回目标字符串开头不包含源字符串字符的数目。strpbrk()
: 在目标字符串中查找源字符串中任意字符的第一个位置。
1. strncpy()
函数原型:char* strncpy(char* dest, const char* src, size_t count)
该函数可以将源字符串的前count个字符复制到目标字符串中,并返回目标字符串的指针。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[11] = "hello";
char str2[6] = "world";
strncpy(str1, str2, 3);
cout << str1 << " " << str2;
return 0;
}
运行结果为:
worlo world
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[11] = "hello";
char str2[6] = "world";
strncpy(str1, str2, 6);
cout << str1 << " " << str2;
return 0;
}
运行结果为:
world world
2. strncat()
函数原型:char* strncat(char* dest, const char* src, size_t count)
该函数可以将目标字符串和源字符串的前count个字符拼接到一起,并返回目标字符串的指针。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[11] = "hello";
char str2[6] = "world";
strncat(str1, str2, 3);
cout << str1 << " " << str2;
return 0;
}
运行结果为:
hellowor world
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[11] = "hello";
char str2[6] = "world";
strncat(str2, str1, 6);
cout << str1 << " " << str2;
return 0;
}
运行结果为:
hello worldhello
3. strstr()
函数原型:char* strstr(const char* str1, const char* str2)
该函数可以在字符串str1中查找子串str2,并返回第一次找到的位置,如果找不到则返回NULL。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[9] = "find me!";
char str2[4] = "me!";
char* p = strstr(str1, str2);
if (p)
cout << "The substring is found at position " << p - str1 << endl;
else
cout << "The substring is not found.";
return 0;
}
运行结果为:
The substring is found at position 5
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[10] = "find me!!";
char str2[3] = "me";
char* p = strstr(str1, str2);
if (p)
cout << "The substring is found at position " << p - str1 << endl;
else
cout << "The substring is not found.";
return 0;
}
运行结果为:
The substring is found at position 5
4. strspn()
函数原型:size_t strspn(const char* str1, const char* str2)
该函数可以返回字符串str1开头连续包含字符串str2字符的数目。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[9] = "find me!";
char str2[5] = "fide";
size_t len = strspn(str1, str2);
cout << "The length of the matched substring is " << len;
return 0;
}
运行结果为:
The length of the matched substring is 2
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[9] = "find me!";
char str2[5] = "abcd";
size_t len = strspn(str1, str2);
cout << "The length of the matched substring is " << len;
return 0;
}
运行结果为:
The length of the matched substring is 0
5. strcspn()
函数原型:size_t strcspn(const char* str1, const char* str2)
该函数可以返回字符串str1开头不包含字符串str2字符的数目。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[9] = "find me!";
char str2[5] = "mn";
size_t len = strcspn(str1, str2);
cout << "The length of the unmatched substring is " << len;
return 0;
}
运行结果为:
The length of the unmatched substring is 4
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[9] = "find me!";
char str2[5] = "fi";
size_t len = strcspn(str1, str2);
cout << "The length of the unmatched substring is " << len;
return 0;
}
运行结果为:
The length of the unmatched substring is 0
6. strpbrk()
函数原型:char* strpbrk(const char* str1, const char* str2)
该函数可以在字符串str1中查找字符串str2中任意字符的第一个位置,并返回该位置的指针。
示例1:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[10] = "findme!";
char str2[5] = "lm";
char* p = strpbrk(str1, str2);
if (p)
cout << "The first matching character is " << *p << endl;
else
cout << "No matching character found.";
return 0;
}
运行结果为:
The first matching character is m
示例2:
#include <cstring>
#include <iostream>
using namespace std;
int main() {
char str1[10] = "findme!";
char str2[5] = "xy";
char* p = strpbrk(str1, str2);
if (p)
cout << "The first matching character is " << *p << endl;
else
cout << "No matching character found.";
return 0;
}
运行结果为:
No matching character found.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:C++常用字符串函数大全(2) - Python技术站