C++字符串处理与转换

string

string字符串处理

长度:str.length()
尺寸:str.size()
比较:str1.compare(str2)
连接:str1+=str2
连接:str1.append(str2)
提取:str2=str2.substr(pos1)
查找:pos=str1.find(str2)
插入:str1.insert(pos1,str2);
替换:str1.replace(pos1,str2);
删除:str1.erase(pos,len)
清除:str.clear()

string转数字

stoi
#include<string>
#include<iostream>
using namespace std;
int main() {
	string s = "10";
	int a;
	a = stoi(s, 0, 10);	//把字符串s从p开始转换成b进制的int
	cout << a;
}
stringstream
#include<string>
#include<iostream>
#include<sstream>
using namespace std;
int main() {
	string s = "10";
	int a;
	stringstream ss;
	ss << s;
	ss >> a;
	printf("%d", a);
}

数字转字符串:to_string()

#include<string>
#include<cstdio>
using namespace std;
int main() {
	int val = 10;
	string s = to_string(val);
	printf("%s", s);
}

string转c字符串

#include<string>
#include<cstdio>
using namespace std;
int main() {
	char ar[] = "hello";
	string s = ar;
	printf("%s", s.c_str());
}

C-风格字符串

字符串转数字

atoi
#include<cstdlib>
#include<cstdio>
// ascii to integer/float/long long
int main() {
	char num1[] = "123";
	int a = atoi(num1);

	char num2[] = "8589934592.4";
	double b = atof(num2);

	char num3[] = "8589934592";
	long long c = atoll(num3);

	printf("%d\n%lf\n%lld", a, b, c);
}
sscanf

使用方法和scanf类似,从字符串提取数字,第一个参数是字符串,第二个参数指定提取格式,第三个指定存数的变量。

#include<cstdio>
int main() {
	string s = "32 is the parent of 11";
	int a, b;
	sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
}

数字转字符串

sprintf
#include<cstdio>
int main() {
	char buffer[50];
	int a = 0x7fffffff, b = 3;
	sprintf(buffer, "%d", a);
	printf("%s\n", buffer);
	sprintf(buffer, "%d", b);
	printf("%s", buffer);
}
itoa
#include<cstdio>
#include<cstdlib>
int main() {
	char buffer[50];
	int a = 0x7fffffff, b = 3;
	_itoa(a, buffer, 10);
	printf("%s\n", buffer);
	_itoa(b, buffer, 2);	//指定进制
	printf("%s", buffer);
}

其它

函数名: strcpy
功 能: 将参数src字符串拷贝至参数dest所指的地址
用 法: char *strcpy(char *dest, const char *src);
返回值: 返回参数dest的字符串起始地址
说 明: 如果参数dest所指的内存空间不够大,可能会造成缓冲溢出的错误情况,在编写程序时需特别留意,或者用strncpy()来取代;
程序例:

#include <stdio.h>
#include <string.h>

int main(void)
 {
    char string[10];
    char *str1 = "abcdefghi";

    strcpy(string, str1);
    printf("%s\n", string);  // 输出:abcdefghi
    return 0;
 }

函数名: strncpy
功 能: 将字符串src前n个字符拷贝到字符串dest
用 法: char *strncpy(char *dest, const char *src, size_t n);
返回值: 返回参数dest的字符串起始地址
说 明: 不像strcpy()strncpy()不会向dest追加结束标记’\0’;
srcdest所指的内存区域不能重叠,且dest必须有足够的空间放置n个字符;
程序例:

#include <stdio.h>
#include <string.h>

int main(void)
{
   char string[10];
   char *str1 = "abcdefghi";

   strncpy(string, str1, 3);
   string[3] = '\0';
   printf("%s\n", string);  // 输出:abc
   return 0;
}

函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *dest, const char *src);
返回值: 返回dest字符串起始地址
说 明: strcat() 会将参数src字符串复制到参数dest所指的字符串尾部;
dest最后的结束字符’\0’会被覆盖掉,并在连接后的字符串的尾部再增加一个’\0’;
destsrc所指的内存空间不能重叠,且dest要有足够的空间来容纳要复制的字符串;
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
{
   char destination[25];
   char *blank = " ", *c = "C++", *Borland = "Borland";

   strcpy(destination, Borland);
   strcat(destination, blank);
   strcat(destination, c);

   printf("%s\n", destination);  // 输出:Borland C++
   return 0;
}

函数名: strncat
功 能: 将n个字符追加到字符串的结尾
用 法: char *strncat(char *dest, const char *src, size_t n);
返回值: 返回dest字符串起始地址
说 明: strncat()将会从字符串src的开头拷贝n个字符到dest字符串尾部,dest要有足够的空间来容纳要拷贝的字符串;
如果n大于字符串src的长度,那么仅将src全部追加到dest的尾部;
strncat()会将dest字符串最后的’\0’覆盖掉,字符追加完成后,再追加’\0’;
程序例:

#include<stdio.h>
#include<string.h>

int main()
{
    char url[100] = "http://blog.csdn.net";
    char path[30] = "/cpp/u/string/";
    strncat(url, path, 1000);  // 1000远远超过path的长度
    printf("%s\n", url);  // 输出;http://blog.csdn.net/cpp/u/string/
    return  0;
}

函数名: strchr
功 能: 在一个字符串中查找给定字符的第一个匹配之处
用 法: char *strchr(const char *str, int c);
返回值: 如果找到指定的字符则返回该字符所在地址,否则返回NULL
说 明: 返回的地址是字符串在内存中随机分配的地址再加上你所搜索的字符在字符串的位置;
字符串str的结束标志‘\0’也会被纳入检索范围,所以str的最后一个字符也可以被定位;
如果希望查找某字符在字符串中最后一次出现的位置,可以使用 strrchr() 函数;
程序例:

#include <stdio.h>
#include <string.h>

int main()
{
    char *s = "0123456789012345678901234567890";
    char *p;
    p = strchr(s, '5');  
    printf("%ld\n", s);  // 输出:134513940
    printf("%ld\n", p);  // 输出:134513945
    p = strrchr(s, '5');
    printf("%ld\n", p);  // 输出:134513965
    return 0;
}

函数名: strcmp
功 能: 字符串比较
用 法: int strcmp(const char *s1, const char *s2);
返回值: 根据ASCII码比较,若参数s1s2字符串相同则返回0,s1若大于s2则返回大于0的值,s1若小于s2则返回小于0的值
说 明: 它是区分大小写比较的,如果希望不区分大小写进行字符串比较,可以使用stricmp函数
程序例:

#include <string.h>
#include <stdio.h>

int main(void)
 {
    char *a = "aBcDeF";
    char *b = "AbCdEf";
    char *c = "aacdef";
    char *d = "aBcDeF";
    printf("strcmp(a, b) : %d\n", strcmp(a, b));  // 输出:1
    printf("strcmp(a, c) : %d\n", strcmp(a, c));  // 输出:-1
    printf("strcmp(a, d) : %d\n", strcmp(a, d));  // 输出:0
    return 0;
 }

函数名: strlen
功 能: 计算指定的字符串s的长度,不包括结束字符’\0’
用 法: size_t strlen(const char *s);
返回值: 返回字符串s的字符数
说 明: strlen() 函数计算的是字符串的实际长度,遇到第一个’\0’结束;
如果你只定义没有给它赋初值,这个结果是不定的,它会从首地址一直找下去,直到遇到’\0’停止;
sizeof返回的是变量声明后所占的内存数,不是实际长度,此外sizeof不是函数,仅仅是一个操作符,strlen()是函数;
程序例:

#include<stdio.h>
#include<string.h>

int main()
{
    char str[5] = "abcd";
    printf("strlen(str)=%d, sizeof(str)=%d\n", strlen(str), sizeof(str));  // 输出:strlen(str)=4, sizeof(str)=5
    return 0;
}

函数名: strspn
功 能: 用来计算字符串str中连续有几个字符都属于字符串 accept
用 法: size_t strspn(const char *str, const char * accept);
返回值: 返回字符串str开头连续包含字符串accept内的字符数目。所以,如果str所包含的字符都属于accept,那么返回str的长度;如果str的第一个字符不属于accept,那么返回0
说 明: 检索的字符是区分大小写的;
函数 strcspn() 的含义与 strspn() 相反,它用来用来计算字符串str中连续有几个字符都不属于字符串accept
程序例:
(1)

#include <stdio.h>
#include <string.h>

int main ()
{
    int i;
    char str[] = "129th";
    char accept[] = "1234567890";

    i = strspn(str, accept);
    printf("str 前 %d 个字符都属于 accept\n",i);  // 输出:str 前 3 个字符都属于 accept
    return 0;
}

(2)一个strcspn()的例子

#include<stdio.h>
#include<string.h>

int main()
{
    char* s1 = "http://c.biancheng.net/cpp/xitong/";
    char* s2 = "z -+*";

    if(strlen(s1) == strcspn(s1,s2)){
        printf("s1 is diffrent from s2!\n");  // 输出:s1 is diffrent from s2!
    }else{
        printf("There is at least one same character in s1 and s2!\n");
    }
    return 0;
}

函数名: strtok
功 能: 根据分界符将字符串分割成一个个片段
用 法: char *strtok(char *s, const char *delim);
返回值: 返回下一个分割后的字符串指针,如果已无从分割则返回NULL
说 明: 当strtok()在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为’\0’字符;
在第一次调用时,strtok()必须赋予参数s字符串,往后的调用则将参数s设置成NULL
程序例:

#include <stdio.h>
#include <string.h>

int main()
{
    char s[] = "ab-cd : ef";
    char *delim = "-: ";
    char *p;
    printf("%s \n", strtok(s, delim));
    // 输出:ab
    //      cd
    //      ef
    while((p = strtok(NULL, delim)))
        printf("%s ", p);
        printf("\n");
}

函数名: strstr
功 能: 检索子串在字符串中首次出现的位置
用 法: char *strstr( char *str, char * substr );
返回值: 返回字符串str中第一次出现子串substr的地址;如果没有检索到子串,则返回NULL
程序例:

#include<stdio.h>
#include<string.h>
int main(){
    char *str = "HelloWorldHelloWorld";
    char *substr = "World";
    char *s = strstr(str, substr);  
    printf("%s\n", s);  // 输出:WorldHelloWorld
    return 0;
}

函数名: strpbrk
功 能: 返回两个字符串中首个相同字符的位置
用 法: char *strpbrk(char *s1, char *s2);
返回值: 如果s1s2含有相同的字符,那么返回指向s1中第一个相同字符的指针,否则返回NULL
说 明: strpbrk()不会对结束符’\0’进行检索
程序例:

#include<stdio.h>
#include<string.h>

int main(){
    char* s1 = "see you again";
    char* s2 = "you";
    char* p = strpbrk(s1,s2);
    if(p){
        printf("The result is: %s\n",p);  // 输出:The result is: you again  
    }else{
        printf("Sorry!\n");
    }
    return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值