字符/字符串操作函数(C)

来源:互联网 发布:身高数据呈现 编辑:程序博客网 时间:2024/05/17 07:00

isdigit

Tests whether an element in a locale is a numeric character.
检查指定元素是否是数字
原型:
template
bool isdigit(
CharType _Ch,
const locale& _Loc //国家–语种
)

示例:

include
include
using namespace std;

int main( )
{
locale loc ( “German_Germany” );
bool result1 = isdigit ( ‘L’, loc );//false
bool result2 = isdigit ( ‘@’, loc );//false
bool result3 = isdigit ( ‘3’, loc );//true
}

isalpha

//检测指定函数是否是字母 用法,原型同上

isalnum

//检测指定函数是否是字母或者数字,用法、原型同上

toupper、tolower

toupper:把指定元素转成大写
tolower:把指定元素转成小写
原型:
template
CharType toupper(
CharType _Ch,
const locale& _Loc
)
示例:
int main( )
{
locale loc ( “German_Germany” );
char result1 = toupper ( ‘h’, loc );//H
char result2 = toupper ( ‘H’, loc );//H
char result3 = toupper ( ‘,loc);//
}
同理,tolower

sprintf_s

//格式化一个字符串,比sprintf更加安全。
//示例
char buffer[20];
int a = 2, b = 3;
sprintf_s(buffer, _countof(buffer), TEXT(“加法:%d+%d=%d”), a, b, a + b);

atoi | atof

字符串转成整数或者浮点数
char buf[] = “1234578”;
int nNum=atoi(buf);//1234578
float nFloat = atof(buf); //1234578.00

strlen

//返回指定字符串的实际长度,不包括‘\0’;
int i = strlen(str1);

strcpy_s

//复制字符串
errno_t strcpy_s(
char *strDestination,//目标
size_t numberOfElements,//目标缓冲区的大小
const char *strSource //源
);
//示例
include

strcmp

int strcmp(
const char *string1,
const char *string2
);
//比较两个字符串是否相等
这里写图片描述

strstr

//在一个字符串中查找另一个字符串并返回目标地址
char *strstr(
const char *str,
const char *strSearch
); // C only

strtok_s

//用指定的标志来分割字符串
原型:
char *strtok_s(
char *strToken, //需要分割的字符串
const char *strDelimit,//用什么来做这个分割标记
char **context //out 指向剩下的字符串
);
示例:

// crt_strtok_s.c// In this program, a loop uses strtok_s// to print all the tokens (separated by commas// or blanks) in two strings at the same time.//#include <string.h>#include <stdio.h>char string1[] =    "A string\tof ,,tokens\nand some  more tokens";char string2[] =    "Another string\n\tparsed at the same time.";char seps[]   = " ,\t\n";char *token1 = NULL;char *token2 = NULL;char *next_token1 = NULL;char *next_token2 = NULL;int main( void ){    printf( "Tokens:\n" );    // Establish string and get the first token:    token1 = strtok_s( string1, seps, &next_token1);    token2 = strtok_s ( string2, seps, &next_token2);    // While there are tokens in "string1" or "string2"    while ((token1 != NULL) || (token2 != NULL))    {        // Get next token:        if (token1 != NULL)        {            printf( " %s\n", token1 );            token1 = strtok_s( NULL, seps, &next_token1);        }        if (token2 != NULL)        {            printf("        %s\n", token2 );            token2 = strtok_s (NULL, seps, &next_token2);        }    }}

_strupr_s

//把字串转成大写
原型:
errno_t _strupr_s(
char *str,
size_t numberOfElements
);
示例:

char string1[] =”A string\tof ,,tokens\nand some more tokens”;
_strupr_s(string1, sizeof(string1));
//全部转成大写字母显示

_strlwr_s

//把字串转成小写
用法与原型同上。

0 0
原创粉丝点击