字符串函数,慢慢归类 I ……

本文介绍了字符串处理的基础概念,包括字符串的定义及真实长度获取方法,并详细列举了多种字符串处理函数,如查找、复制、拼接等操作的具体用法。通过实例展示了如何使用strchr()函数定位特定字符。

A string is a continuous sequence of characters terminated by '\0', the string terminator character. The length of a string is considered to be the number of characters before the string terminator.

一个字符串的真实长度可以通过函数len=strlen(str)来获得,而sizeof的则是连着最后的'\0'一起统计在内,关于sizeof的使用会有专门的随笔讨论,在这就先不详细谈论了。

 

Table 16-16. String-processing functions

Purpose

Functions in string.h

Functions in wchar.h

Find the length of a string.

strlen( )

wcslen( )

Copy a string.

strcpy( ), strncpy( )

wcscpy( ), wcsncpy( )

Concatenate strings.

strcat( ), strncat( )

wcscat( ), wcsncat( )

Compare strings.

strcmp( ), strncmp( ), strcoll( )

wcscmp( ), wcsncmp( ), wcscoll( )

Transform a string so that a comparison of two transformed strings using strcmp( ) yields the same result as a comparison of the original strings using the locale-sensitive function strcoll( ).

strxfrm( )

wcsxfrm( )

In a string, find:

   

 

  • The first or last occurrence of a given character

strchr( ), strrchr( )

wcschr( ), wcsrchr( )

 

  • The first occurrence of another string

strstr( )

wcsstr( )

 

  • The first occurrence of any of a given set of characters

strcspn( ), strpbrk( )

wcscspn( ), wcspbrk( )

 

  • The first character that is not a member of a given set

strspn( )

wcsspn( )

Parse a string into tokens

strtok( )

wcstok( )

 一下是一些函数的例子

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

const char * strchr ( const char * str, int character );  

char * strchr ( char * str, int character );

Parameters

str
C string.
character
Character to be located. It is passed as its  int promotion, but it is internally converted back to  char.

Return Value

A pointer to the first occurrence of  character in  str.
If the  value is not found, the function returns a null pointer.

ContractedBlock.gifExpandedBlockStart.gif代码
1 /* strchr example */
2 #include <stdio.h>
3 #include <string.h>
4
5  int main ()
6 {
7 char str[] = "This is a sample string";
8 char * pch;
9 printf ("Looking for the 's' character in \"%s\"...\n",str);
10 pch=strchr(str,'s');
11 while (pch!=NULL)
12 {
13 printf ("found at %d\n",pch-str+1);
14 pch=strchr(pch+1,'s');
15 }
16 return 0;
17 }

 

 

Output:

Looking for the 's' character in "This is a sample string"...
found at 4
found at 7
found at 11
found at 18
慢慢整理……先这么多

先说点它的使用方法

1.for(int i=0;i<strlen(buf);i++)

  if(strchr(Set,buf[i])==NULL)    code……

这是用来检测buf中的每个字符是否为是Set中的字符。这个很有用处

2.向刚才举的那个例子,统计某个字符在某个串中出现的次数,或这位置之类的


转载于:https://www.cnblogs.com/aluenkinglee/archive/2010/07/28/1787048.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值