c语言CString转数字函数,cstring的常用函数

本文描述了cstring的常用函数的相关知识,对类似的源码问题比较有借鉴作用,感兴趣的可以了解细看。

总结常用的

getline(cin,a)//可以读入空格

// == != < >

insert() // 插入

erase() // 删除

replace() //替换

find()

rfind() // 两个查找

首先是string 头文件

#include

#include

只要有这两个 就有string函数了

最最基本的

string s, str; // 声明变量

str = "asdfghjkl" // 直接赋值

cin >> s; // 遇到空格结束输入

getline(cin, s); // 遇到换行结束输入 用于要输入空格的情况

cout << s; // 输出

int len = s.length(); // 得到字符串长度

swap(s, str);// 交换 s str

printf("%s\n",s.c_str()); //C风格输出,c_str()是啥?

c_str()

返回一个char *, char类型的指针

简单的说就是把 string类型当char字符串输出

但是正常没有人会这样做吧 转来转去的 多麻烦~~

要不一直用string 要不一直用char[]

重载方面

就是把string当一般类型用

连接两个string

string a = "123";

string b = "456";

string c = ab;

// c = "123456"

c = a b c;

// c = "123456123456"

根据字典树比较 > < == !=

inline bool cmp (string a, string b) {

return a > b;

}

int main() {

----

bool flag = cmp(a, b);

}

insert() 插入

在某个string 的指定位置插入另一个string

string.insetr(int, string);

string str="to be question";

string str2="the ";

str.insert(6,str2); // to be (the )question

erase() 删除

从指定位置开始删除n个字符,比如erase(0,1)就是删除第一个字符

string.erase(int, int);

string str="to be the question";

str.erase(6, 4); // to be question

clear() 清除

直接把string清除… 应该不用说了吧

string str="to be the question";

str.clear(); // 变成了空串""

replace() 替换

替换a中指定开始往后n的这些字符变为b

往往与find()一起使用

string.(int, int, string);

string str="to be the question";

str.replace(6, 3, "THE"); // to be THE question

string s = "THe to q";

str.replace(6, 3, s);// to be THe question

find()与rfind()

完全匹配String b

a.find(b) 从开始找b第一次出现的位置并返回

a.find(b,pos) 从pos开始找b第一次出现的位置并返回

string.find(string); string.find(string, int);

string str="To be, or not to be - that is the question";

int t=str.find("be");\\ t=3,str[t]='b'(To be 的be)

int t=str.find("be",4);\\ t=17,str[t]='b'(not to be的be)

rfind(b)或rfind(b,pos) 就是倒着找O(∩_∩)O~

a.efind(b) 从末尾找b第一次出现的位置并返回

a.rfind(b,pos) 从pos为末尾开始找b第一次出现的位置并返回

string.rfind(string); string.rfind(string, int);

string str="To be, or not to be - that is the question";

int t=str.rfind("be");\\ t=17,str[t]='b'(not to be 的be)

int t=str.rfind("be",15);\\ t=3,str[t]='b'(To be的be)

没有出现,返回npos,即-1(打印出来为4294967295)

if (str.find("BE")==string::npos)

cout <

if (str.rfind("BE")==-1)

cout <

find_first_of()与find_last_of()

a.find_first_of(b) 或 a.find_last_of(b,pos)

**string.find_first_of(string, int)**正着找

**string.find_last_of(string, int)**倒着找

在a开始(或从pos开始)向后查找,只要在a中遇到一个字符,该字符与c中任意一个字符相同,就停止查找,返回该字符在a中的位置;若匹配失败,返回npos

string str="To be, or not to be - that is the question";

string::size_type s = str.find_first_of("aeiou")"abcde"

while(s != string::npos) {

str[s] = '*';

s = str.find_first_of("aeiou", s 1);

}

cout << str << endl;

// "T* b*, *r n*t t* b* - th*t *s th* q**st**n"

就是找第一个相同的字符嘛

感觉是用不到这个了解了解就行了不了解也可以的

find_first_not_of()与find_last_not_of()

感觉和前面一类的相反的,类似于找了个补集。即在a中搜寻b中没有的字符并返回位置

// 就像这样

string str="To be, or not to be - that is the question";

string::size_type s = str.find_first_of("aeiou")"abcde"

while(s != string::npos) {

str[s] = '*';

s = str.find_first_of("aeiou", s 1);

}

cout << str << endl;

// "*o *e, o* *o* *o *e - **a* i* **e *ue**io*"

感觉一样是用不到这个了解了解就行了不了解也可以的

substr() 字串

substr(int, int);

保留某一段string

第二个参数可以不写

string str="To be, or not to be - that is the question";

str.substr(0,2);// To

str.substr(str.find("question"));// question

string与int互转(不考虑C 11的函数)

int转string

ostringstream outs; //输出字符串流

int x = 12;

outs << x; //向输出字符串流中输出x的内容

string a=outs.str(); //利用字符串流的str函数获取流中的内容

string转int

string a="12";

istringstream ins(a); //输入字符串流,流的内容初始化为a

int x;

ins >> x; //从is流中读入并存入x中

没卵用的东西

String与char的转换

String转char*

data()

string str = "hello";

const char* p = str.data();

//加const 或者用char * p=(char*)str.data();的形式

同时有一点需要说明,这里在devc 中编译需要添加const,否则会报错invalid conversion from const char* to char

c_str()string str=“world”;

const char *p = str.c_str();

//同上,要加const或者等号右边用char*

copy()

string str="hmmm";

char p[50];

str.copy(p, 5, 0);//这里5代表复制几个字符,0代表复制的位置,

*(p5)=‘\0’;//注意手动加结束符!!

String转char[ ],直接循环赋值

string pp = "dagah";

char p[8];

int i;

for( i=0;i

p[i] = pp[i];

p[i] = '\0';

关键词:c ,字符串,string,常用,函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值