常用C字符串操作函数
来源:互联网 发布:ni 6229数据手册 编辑:程序博客网 时间:2024/06/03 13:51
int
strcmp(const char
*
string1,
const
char
*
string2);
//比较字符串函数.
宽字符版本:int
wcscmp(const wchar_t
*string1, const wchar_t
*string2 );
返回值:string1等于string2 0,string1大于string2 > 0,string1小于string2 < 0
char
*strcpy(char
* strDestination, char
* strSource); //复制字符串
宽字符版本:wchar_t
*wcscpy(wchar
_t
*strDestination, wchar_t
*strSource);
返回值:指向strDestination的指针
char
*strstr
(char
*str1,
const
char
*str2
); 字符串查找函数
宽字符版本:wchar_t
*wcsstr(const wchar_t
*str1,const wchar_t
*str2 );
返回值:若str2是str1的子串,则先确定str2在str1的第一次出现的位置,并返回此str1在str2首位置的地址。;如果str2不是str1的子串,则返回NULL。
char
*strtok
(char
*s,const
char
*delim);
功能:分解字符串为一组标记串。s为要分解的字符串,delim为分隔符字符串。
说明:strtok()用来将字符串分割成一个个片段。当strtok()在参数s的字符串中发现到参数delim的分割字符时则会将该字符改为 \0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回被分割出片段的指针。当没有被分割的串时则返回NULL。所有delim中包含的字符都会被滤掉,并将被滤掉的地方设为一处分割的节点。
举例:
/* strtokexample */
#include<stdio.h>
#include<string.h>
int main (void)
{
char str[] = "- This, a samplestring.";
char *pch;
printf("Splitting string\"%s\" into tokens:\n", str);
pch = strtok(str," ,.-");
while (pch != NULL)
{
printf("%s\n", pch);
pch = strtok(NULL, " ,.-");
}
printf("at the end: %s", str);
return 0;
}
Splitting string"- This, a sample string." into tokens:
This
a
sample
string
the end: - This
注:strtok函数会破坏被分解字符串的完整,调用前和调用后的s已经不一样了。另外貌似制表符 \t 无法充当分割字符。
char
*strchr( const
char
*str,int
ch );
功能:查找字符串 str中首次出现字符 ch的位置
说明:返回首次出现 ch的位置的指针,如果 str中不存在 ch则返回NULL。
举例:
/* strchrexample */
#include<stdio.h>
#include<string.h>
int main ()
{
char str[] = "This is a simplestring";
char *pch;
printf("Looking for the 's' characterin \"%s\"...\n", str);
pch = strchr(str, 's');
while (pch != NULL){
printf("found at %d th\n",pch - str + 1);
pch = strchr(pch + 1, 's');
}
return 0;
}
Looking for the's' character in "This is a simple string"...
found at 4 th
found at 7 th
found at 11 th
found at 18 th
char
*strcat( char
* dest,const
char
* src );
功能:把 src所指字符串添加到 dest结尾处(覆盖dest结尾处的'\0')并添加'\0'。
说明:src和 dest所指内存区域不可以重叠且 dest必须有足够的空间来容纳 src的字符串。
返回指向 dest的指针。
类似的 strncat
char
*strncat( char
* dest,const
char
* src,size_t
num );
int
atoi(constchar*
nptr); 将字符串转换为整形数值
宽字符版本:int
_wtoi(constwchar_t*
nptr);
说明:参数nptr字符串,如果第一个非空格字符存在,是数字或者正负号则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。否则,返回零。
char
*itoa(
int
value,
char
*string,
int
radix);
说明:将任意类型的数字转换为字符串。radix,进制数 返回值:string
int
sprintf(char
*buffer,const
char
*format, [ argument] … );
宽字符版本:int
wsprintf(wchar_t
*buffer,const wchar_t
*format, [ argument] … );
说明:buffer:char型指针,指向将要写入的字符串的缓冲区。format:格式化字符串。[argument]...:可选参数,可以是任何类型的数据。返回值:写入的长度,但不包括最后的'\0'
转换字符:%%印出百分比符号,不转换。%c整数转成对应的 ASCII字元。%d整数转成十进位。%f倍精确度数字转成浮点数。%o整数转成八进位。%s整数转成字符串。%x整数转成小写十六进位。%X整数转成大写十六进位。
- 常用C字符串操作函数
- 常用C语言字符串操作函数
- 常用C语言字符串操作函数
- C语言常用字符串操作函数
- 常用C语言字符串操作函数
- C语言常用字符串操作函数总结
- C语言常用字符串操作函数总结
- C——常用字符串函数、内存操作函数总结
- 字符串操作常用函数
- 字符串操作常用函数
- 常用字符串操作函数
- 常用字符串操作函数
- 常用字符串操作函数
- 常用字符串操作函数
- C 常用字符串函数
- C# 常用字符串函数
- c常用字符串函数
- 常用C字符串函数
- 【CSS3】属性选择器
- php emoji表情处理
- 安卓屏屏幕适配
- 梦行文档(在线手册制作系统,帮助手册,教程说明文档,使用手册说明)
- Nginx1.8.1+Tomcat7.0负载均衡
- 常用C字符串操作函数
- pc进入android手机shell的root权限(su: not found)
- 动态规划学习笔记一
- CentOS7安装iptables防火墙
- android跨进程启动service
- Android自定义view-弹性ScrollView(下)
- ionic android版APP tab
- iOS视图pop出栈
- popupwindow全屏幕显示被状态栏挡住如何解决+Android获取状态栏高度