C++标准库-cstring【字符串处理总结】

总感觉字符串处理是很重要的,从做那么多题来看,感觉某些地方还是不熟练,似懂非懂,由于我不想过多的学其他的语言如c#和Java,尽管我知道她们内置的某些字符串处理函数比c/c++方便的多,但是我还是打算先把c/c++里的字符串处理弄清楚。

首先假如你只使用纯c来进行字符串处理,那你只能采用char[]方案,采用string.h(在c++里称为cstring)里内置的函数。这种方案的优点是效率高,缺点是有指针类型不安全且复杂难于理解。
若使用c++,在知乎上看了一下好像大家都推荐使用string类来进行字符串处理,使用STL中的容器和迭代器以及#include,这种方案优点好像比char的那个容易理解,而且类型更安全,但效率比char低。

注意这里是cstring,不是MFC的CString类,cstring是由c里面的string.h升级而来。
函数:
1.memcpy
void * memcpy ( void * destination, const void * source, size_t num );
把source所指的内容的前num位赋值给destination例:

1
2
3
4
5
6
7
8
9
10
11
12
#include
#include
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}

输出
str1: Sample string
str2: Sample string
str3: copy successful

2.memmove
void * memmove ( void * destination, const void * source, size_t num );
把source的后num位所指的内容赋给destination后num位所指的内容。

1
2
#include
#include

int main ()
{
char str[] = “memmove can be very useful……”;
memmove (str+20,str+15,11);
puts (str);
return 0;
}

输出:memmove can be very very useful.

3.memcmp
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
比较ptr1和ptr2前num个字符的大小

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include
#include
main()
{
char *s1 = "Hello,Programmers!";
char *s2 = "Hello,Programmers!";
int r;
= memcmp(s1,s2,strlen(s1));
if(!r)
printf("s1 and s2 are identical\n");/*s1等于s2*/
elseif(r<0)
printf("s1 is less than s2\n");/*s1小于s2*/
else
printf("s1 is greater than s2\n");/*s1大于s2*/
return 0;
}

输出结果:
s1 and s2 are identical
请按任意键继续…

4.memchr
const void * memchr ( const void * ptr, int value, size_t num );
void * memchr ( void * ptr, int value, size_t num );
查ptr前num位ascii码为value的位置,没有查到返回NULL

1
2
#include
#include

int main ()
{
char * pch;
char str[] = “Example string”;
pch = (char*) memchr (str, ‘p’, strlen(str));
if (pch!=NULL)
printf (“‘p’ found at position %d.\n”, pch-str+1);
else
printf (“‘p’ not found.\n”);
return 0;
}

Output:

‘p’ found at position 5.

 

5.memset
void * memset ( void * ptr, int value, size_t num );
在ptr前插入相同的ASCII码值为value的字符num个

1
2
#include
#include

int main ()
{
char str[] = “almost every programmer should know memset!”;
memset (str,’-‘,6);
puts (str);
return 0;
}

Output:

—— every programmer should know memset!

 

6.strcat
char * strcat ( char * destination, const char * source );
把source连接在destination后面


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[80];
strcpy (str,”these “);
strcat (str,”strings “);
strcat (str,”are “);
strcat (str,”concatenated.”);
puts (str);
return 0;
}

Output:

these strings are concatenated.

 

7.strchr
const char * strchr ( const char * str, int character );
char * strchr ( char * str, int character );
返回str中character所在的指针值


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] = “This is a sample string”;
char * pch;
printf (“Looking for the ‘s’ character in \”%s\”…\n”,str);
pch=strchr(str,’s’);
while (pch!=NULL)
{
printf (“found at %d\n”,pch-str+1);
pch=strchr(pch+1,’s’);
}
return 0;
}

Output:

Looking for the ‘s’ character in “This is a sample string”…found at 4found at 7found at 11found at 18

 

8.strcmp
int strcmp ( const char * str1, const char * str2 );
比较两者关系,若str1==str2,则返回零;
若str1<str2,则返回负数; 若str1>str2,则返回正数。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char szKey[] = “apple”;
char szInput[80];
do {
printf (“Guess my favourite fruit? “);
gets (szInput);
} while (strcmp (szKey,szInput) != 0);
puts (“Correct answer!”);
return 0;
}

Output:

Guess my favourite fruit? orangeGuess my favourite fruit? appleCorrect answer!

 

9.strcoll
int strcoll ( const char * str1, const char * str2 );
默认情况下(LC_COLLATE为”POSIX”或”C”)和strcmp一样根据ASCII比较字符串大小。
对于设置了LC_COLLATE语言环境的情况下,则根据LC_COLLATE设置的语言排序方式进行比较。例如:汉字,根据拼音进行比较。

10.strcpy
char * strcpy ( char * destination, const char * source );
将source复制到destination


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str1[]=”Sample string”;
char str2[40];
char str3[40];
strcpy (str2,str1);
strcpy (str3,”copy successful”);
printf (“str1: %s\nstr2: %s\nstr3: %s\n”,str1,str2,str3);
return 0;
}

Output:

str1: Sample stringstr2: Sample stringstr3: copy successful

 

11.strcstn
size_t strcspn ( const char * str1, const char * str2 );
函数说明:strcspn()从参数str1 字符串的开头计算连续的字符, 而这些字符都完全不在参数str2 所指的字符串中. 简单地说, 若strcspn()返回的数值为n, 则代表字符串str1 开头连续有n 个字符都不含字符串str2 内的字符.
返回值:返回字符串str1 开头连续不含字符串str2 内的字符数目


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] = “fcba73”;
char keys[] = “1234567890”;
int i;
i = strcspn (str,keys);
printf (“The first number in str is at position %d.\n”,i+1);
return 0;
}

Output:

The first number in str is at position 5

 

12.strerror
char * strerror ( int errnum );
获取系统错误信息或打印用户程序错误信息。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
FILE * pFile;
pFile = fopen (“unexist.ent”,”r”);
if (pFile == NULL)
printf (“Error opening file unexist.ent: %s\n”,strerror(errno));
return 0;
}

A possible output:
Error opening file unexist.ent: No such file or directory

 

13.strlen
size_t strlen ( const char * str );
获取字符串长度


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

#include
#include

int main ()
{
char szInput[256];
printf (“Enter a sentence: “);
gets (szInput);
printf (“The sentence entered is %u characters long.\n”,strlen(szInput));
return 0;
}
Output:

Enter sentence: just testingThe sentence entered is 12 characters long.

/code>

14.strncat
char * strncat ( char * destination, char * source, size_t num );
strncat()将会从字符串source的开头拷贝num 个字符到destination字符串尾部,destination要有足够的空间来容纳要拷贝的字符串。如果num大于字符串source的长度,那么仅将source指向的字符串内容追加到dest的尾部。
strncat()会将dest字符串最后的’\0’覆盖掉,字符追加完成后,再追加’\0’。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str1[20];
char str2[20];
strcpy (str1,”To be “);
strcpy (str2,”or not to be”);
strncat (str1, str2, 6);
puts (str1);
return 0;
}

Output:

To be or not

 

15.strncmp
int strncmp ( const char * str1, const char * str2, size_t num );
把str2的前num位于str1进行比较。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[][5] = { “R2D2” , “C3PO” , “R2A6” };
int n;
puts (“Looking for R2 astromech droids…”);
for (n=0 ; n if (strncmp (str[n],”R2xx”,2) == 0)
{
printf (“found %s\n”,str[n]);
}
return 0;
}

Output:

Looking for R2 astromech droids…found R2D2found R2A6

 

16.strncpy
char * strncpy ( char * destination, const char * source, size_t num );
把source的前num位复制到destination。

17.strpbrk
const char * strpbrk ( const char * str1, const char * str2 );
char * strpbrk ( char * str1, const char * str2 );
strpbrk是在源字符串(str2)中找出最先含有搜索字符串(str1)中任一字符的位置并返回,若找不到则返回空指针。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] = “This is a sample string”;
char key[] = “aeiou”;
char * pch;
printf (“Vowels in ‘%s’: “,str);
pch = strpbrk (str, key);
while (pch != NULL)
{
printf (“%c ” , *pch);
pch = strpbrk (pch+1,key);
}
printf (“\n”);
return 0;
}

Output:

Vowels in ‘This is a sample string’: i i a a e i

 

18.strrchr
const char * strrchr ( const char * str, int character );
char * strrchr ( char * str, int character );
查找一个字符c在另一个字符串str中末次出现的位置(也就是从str的右侧开始查找字符c首次出现的位置),并返回这个位置的地址。如果未能找到指定字符,那么函数将返回NULL。使用这个地址返回从最后一个字符c到str末尾的字符串。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] = “This is a sample string”;
char * pch;
pch=strrchr(str,’s’);
printf (“Last occurence of ‘s’ found at %d \n”,pch-str+1);
return 0;
}

Output:

Last occurrence of ‘s’ found at 18

 

19.strspn
size_t strspn ( const char * str1, const char * str2 );
返回字符串中第一个不在指定字符串中出现的字符下标


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
int i;
char strtext[] = “129th”;
char cset[] = “1234567890”;

i = strspn (strtext,cset);
printf (“The length of initial number is %d.\n”,i);
return 0;
}

Output:

The length of initial number is 3.

 

20.strstr
const char * strstr ( const char * str1, const char * str2 );
char * strstr ( char * str1, const char * str2 );
strstr(str1,str2) 函数用于判断字符串str2是否是str1的子串。如果是,则该函数返回str2在str1中首次出现的地址;否则,返回NULL。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] =”This is a simple string”;
char * pch;
pch = strstr (str,”simple”);
strncpy (pch,”sample”,5);
puts (str);
return 0;
}

This example searches for the “simple” substring in str and replaces that word for “sample”.
Output:

This is a sample string

 

21.strtok
char * strtok ( char * str, const char * delimiters )
分解字符串为一组字符串。s为要分解的字符,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。


GeSHi Error: GeSHi could not find the language cquot (using path /home/umbrella/domains/umbrellamrx.com/public_html/wp-content/plugins/codecolorer/lib/geshi/) (code 2)

int main ()
{
char str[] =”- This, a sample string.”;
char * pch;
printf (“Splitting string \”%s\” into tokens:\n”,str);
pch = strtok (str,” ,.-“);
while (pch != NULL)
{
printf (“%s\n”,pch);
pch = strtok (NULL, ” ,.-“);
}
return 0;
}

Output:

Splitting string “- This, a sample string.” into tokens:Thisasamplestring

 

22.strxfrm
size_t strxfrm ( char * destination, const char * source, size_t num );
strxfrm() 函数 返回 除去 结尾的 `\0′ 字符, 用来 存储 转换后的 字符串所 需要的 字节数. 如果 返回值 大于 或 等于 n, dest 中的 内容就是 不确定的.

发表评论

电子邮件地址不会被公开。 必填项已用*标注