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.


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.向刚才举的那个例子,统计某个字符在某个串中出现的次数,或这位置之类的