Keep on going, never give up.

C/C++字符串处理函数

本文汇总C/C++字符串处理函数,方便比较参考。具体用法可参考文末链接文章介绍。

一、C语言中的字符串处理函数

假设预定义变量:

char str1[]="123456789";

char str2[]="HelloWorld!";

1、拷贝类

  • memcpy - Copy block of memory
  • memmove - Move block of memory
  • strcpy - Copy string
  • strncpy - Copy characters from string

2、连接类

  • strcat - Concatenate strings
  • strncat - Append characters from string

3、比较类

  • memcmp - Compare two blocks of memory
  • strcmp - Compare two strings
  • strcoll - Compare two strings using locale
  • strncmp - Compare characters of two strings
  • strxfrm - Transform string using locale

4、搜索类

  • memchr - Locate character in block of memory
  • strchr - Locate first occurrence of character in string,示例:char* p=strchr(str1,'s');
  • strcspn - Get span until character in string
  • strpbrk - Locate character in string
  • strrchr - Locate last occurrence of character in string,示例:char* p=strrchr(str1,'s');
  • strspn - Get span of character set in string
  • strstr - Locate substring
  • strtok - Split string into tokens

5、其它

  • memset - Fill block of memory
  • strerror - Get pointer to error message string
  • strlen - Get string length

二、C++字符串函数

这里是指string类型字符串处理函数。使用时需包含头文件:<string>

假设预定义变量:

string str1="123456789";

string str2="HelloWorld!";

1、遍历类

  • begin - Return iterator to beginning
  • end - Return iterator to end
  • rbegin - Return reverse iterator to reverse beginning
  • rend -Return reverse iterator to reverse end

2、计算类

  • size -Return length of string
  • length -Return length of string
  • max_size -Return maximum size of string
  • resize -Resize string
  • capacity -Return size of allocated storage
  • reserve -Request a change in capacity
  • clear -Clear string
  • empty -Test if string is empty

3、单元访问类

  • operator[] - Get character in string
  • at -Get character in string

4、修改类

  • operator+= Append to string
  • append -Append to string
  • push_back -Append character to string
  • assign -Assign content to string
  • insert -Insert into string
  • erase -Erase characters from string
  • replace -Replace part of string
  • swap -Swap contents with another string

5、字符串操作类

  • c_str -Get C string equivalent
  • data -Get string data
  • get_allocator -Get allocator
  • copy -Copy sequence of characters from string
  • find -Find content in string
  • rfind -Find last occurrence of content in string
  • find_first_of -Find character in string
  • find_last_of -Find character in string from the end
  • find_first_not_of -Find absence of character in string
  • find_last_not_of -Find absence of character in string from the end
  • substr -Generate substring
  • compare -Compare strings

参考资料:

http://www.cplusplus.com/reference/clibrary/cstring/

http://www.cplusplus.com/reference/string/string/

相关评论(0):  

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

订阅博客

最新文章

本站采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载也必须遵循“署名-非商业用途-保持一致”的创作共用协议. 返回顶部
Copyright@2005-2016 Metsky.com, All rights Reserved.