1.string.strcpy()和string.strtok()
char * strcpy ( char * destination, const char * source );
注: destination 指向要复制内容的目标指针;
source 要复制的原目标指针;
char * strtok ( char * str, const char * delimiters );
注: str 字符串截断 请注意,此字符串通过分解为较小的字符串(标记)进行修改
delimiters 包含要分隔的字符串;
例如:
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
string str("Please split,this.sentence");
char cstr[100];
strcpy(cstr, str.c_str()); //对字符串进行复制,注意两个参数的类型是char型
//函数功能是string转换成char
// cstr now contains a c-string copy of str
char * p = strtok(cstr, " ,."); //分割时可以进行不同字符的分割
while (p != 0)
{
cout << p <<endl;
p = strtok(NULL, " ,."); //注意第二次使用时,str为NULL
}
return 0;
}
结果:
please
split
this
sentence
2.string.strstr()
char * strstr (char * str1, const char * str2 );
注:str1 字符串
str2 要匹配的zifuch
此函数用与匹配,查找首次遇到str2字符串的地址
例如:
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="This is a simple string";
char * pch;
pch = strstr (str,"simple");
strncpy (pch,"sample",6);
puts (str);
return 0;
}
结果:
This is a sample string