C语言字符串几个常用函数
2013-03-20 09:31
393 查看
字符串处理函数 1、将格式化数据写入字符串:sprintf
int sprintf( char *buffer, const char *format, ... );
将数据打印到buffer中
例如:char result[100];
int num = 24;
sprintf( result, "%d", num );
例如:char string[50];
int file_number = 0;
sprintf( string, "file.%d", file_number );
file_number++;
output_file = fopen( string, "w" );
又例如:char result[100];
float fnum = 3.14159;
sprintf( result, "%f", fnum );
2、字符串长度查询函数: strlen
int strlen(const char *s);
3、字符串连接函数: strcat
char *strcat(char *dest, const char *src);
4、字符串复制函数:strcpy、strncpy
char *strcpy(char *dest, const char *src);
5、字符串比较函数: strcmp、strncmp、stricmp、strnicmp
字符串比较函数strcmp(分大小写)
int strcmp(const char *s1, const char *s2);
Return Value
Return value
Explanation
less than 0
str1 is less than str2
equal to 0
str1 is equal to str2
greater than 0
str1 is greater than str2''
字符串搜索函数: strcspn、strspn、strstr、strtok、strchr
6、查找子串strstr
char *strstr(char *s1, char *s2);
查找给定字符串在字符串中第一次出现的位置,返回位置指针
如果找到,返回指针,指向s1中第一次出现s2的位置
如果找不到,返回 NULL
pdest = strstr( string, str );
在string中搜索str,返回str在string中第一次出现的位置
例如:char* str1 = "this is a string of characters";
char* str2 = "a string";
char* result = strstr( str1, str2 );
if( result == NULL ) printf( "Could not find '%s' in '%s'\n", str2, str1 );
else printf( "Found a substring: '%s'\n", result );
输出结果为:Found a substring: 'a string of characters'
7、字符串部分拷贝 strncpy, 字符串全部拷贝strcpy
char *strncpy(char *dest, char *src, int maxlen);
char *strcpy(char *dest, char *src);
将前 maxlen 个字符从src拷贝到dest
1)如果src中字符不足 maxlen 个,则连’\0’一起拷贝,’\0’后面的不拷贝
2) 如果src中字符大于等于maxlen个,则拷贝 maxlen个字符
8、搜索字符在串中第一次出现的位置strchr
pdest = strchr( string, ch );
在string中搜索ch,返回str在string中第一次出现的位置
9、字符串大小写转换函数: strlwr、strupr
补充:这些函数都要求 #include <string.h>
int sprintf( char *buffer, const char *format, ... );
将数据打印到buffer中
例如:char result[100];
int num = 24;
sprintf( result, "%d", num );
例如:char string[50];
int file_number = 0;
sprintf( string, "file.%d", file_number );
file_number++;
output_file = fopen( string, "w" );
又例如:char result[100];
float fnum = 3.14159;
sprintf( result, "%f", fnum );
2、字符串长度查询函数: strlen
int strlen(const char *s);
3、字符串连接函数: strcat
char *strcat(char *dest, const char *src);
4、字符串复制函数:strcpy、strncpy
char *strcpy(char *dest, const char *src);
5、字符串比较函数: strcmp、strncmp、stricmp、strnicmp
字符串比较函数strcmp(分大小写)
int strcmp(const char *s1, const char *s2);
Return Value
Return value
Explanation
less than 0
str1 is less than str2
equal to 0
str1 is equal to str2
greater than 0
str1 is greater than str2''
字符串搜索函数: strcspn、strspn、strstr、strtok、strchr
6、查找子串strstr
char *strstr(char *s1, char *s2);
查找给定字符串在字符串中第一次出现的位置,返回位置指针
如果找到,返回指针,指向s1中第一次出现s2的位置
如果找不到,返回 NULL
pdest = strstr( string, str );
在string中搜索str,返回str在string中第一次出现的位置
例如:char* str1 = "this is a string of characters";
char* str2 = "a string";
char* result = strstr( str1, str2 );
if( result == NULL ) printf( "Could not find '%s' in '%s'\n", str2, str1 );
else printf( "Found a substring: '%s'\n", result );
输出结果为:Found a substring: 'a string of characters'
7、字符串部分拷贝 strncpy, 字符串全部拷贝strcpy
char *strncpy(char *dest, char *src, int maxlen);
char *strcpy(char *dest, char *src);
将前 maxlen 个字符从src拷贝到dest
1)如果src中字符不足 maxlen 个,则连’\0’一起拷贝,’\0’后面的不拷贝
2) 如果src中字符大于等于maxlen个,则拷贝 maxlen个字符
8、搜索字符在串中第一次出现的位置strchr
pdest = strchr( string, ch );
在string中搜索ch,返回str在string中第一次出现的位置
9、字符串大小写转换函数: strlwr、strupr
补充:这些函数都要求 #include <string.h>
相关文章推荐
- C语言字符串几个常用函数小结
- C语言字符串几个常用函数小结
- C语言字符串处理的几个常用函数
- C语言中常用的几个字符串处理函数:
- C语言学习笔记之常用的字符串处理函数
- C语言strstr()函数常用示例详解[求一个字符串在另一个字符串中出现的次数]
- 几个常用的用正则表达式验证字符串的函数
- 【C语言】10-字符和字符串常用处理函数
- 自写的几个常用Sql字符串拆分函数
- 常用的字符串处理库函数的C语言实现
- C语言中关于字符串的一些常用函数
- C语言——字符和字符串常用处理函数
- C语言strcspn()函数:检索字符串s1开头连续有几个字符都不含字符串s2中的字符
- 几个常用的JavaScript字符串处理函数 split()、join()、substring()和indexOf()
- 几个常用的JavaScript字符串处理函数 - split()、join()、substring()和indexOf()
- C语言中常用的几个内存申请函数
- 字符串操作常用函数——C语言
- c语言字符串操作,及常用函数
- c语言中常用的字符串处理函数
- 几个常用的JavaScript字符串处理函数 – split()、join()、substring()和indexOf()