str);

发布时间:2019-06-13 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了str);脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

1. 字符数组

字符数组,也就是存放字符类型数据的数组,只不过字符数组的结尾必须是 '0'。C++ 已经提供了一些字符串处理函数,这些函数被封装在头文件 <cstring> 和 <string.h> 中。

1.1. 字符串复制
  • void * memcpy ( void * destination, const void * source, size_t num ); 从 source 指针指向的内存拷贝 num 个字节到 destination 指针指向的内存;拷贝的是二进制数据,与两个指针类型没有关系;不检查字符串结束符,只是拷贝对应字节的数据;两个指针指向的数组大小至少都大于 num 个字节而且不应该有重叠。
  • void * memmove ( void * destination, const void * source, size_t num ); 和 memcpy 类似,只不过支持内存重叠,就好像是有一个中间缓冲区一样。
  • char * strcpy ( char * destination, const char * source ); 将 source 指针指向的字符串拷贝到 destination 指针指向的数组,包括终止字符, destination 指针指向的数组必须有足够的大小来容纳待拷贝字符串,并且不支持内存重叠。
  • char * strncpy ( char * destination, const char * source, size_t num ); 将 source 指针指向的前 num 个字符串拷贝到 destination 指针指向的数组,不支持内存重叠。如果 source 字符串长度大于 num,那么 destination 字符串将没有终止字符;如果 source 字符串长度小于 num,那么 destination 字符串会填充 0 直到总共有 num 个字符写入。
#include <iostream>
#include <stdio.h>
#include <cstring>

using namespace std;

struct {
  char name[40];
  int age;
} person, person_copy;

int main ()
{
    char myname[] = "seniusen";

    /* 用 memcpy 拷贝字符串*/
    memcpy(person.name, myname, strlen(myname)+1);
    person.age = 22;

    /* 用 memcpy 拷贝结构体*/
    memcpy(&person_copy, &person, sizeof(person));

    printf("person_copy: %s, %d n", person_copy.name, person_copy.age );

    /* memmove 支持重叠内存的拷贝*/
    char str[] = "seniusen works very hard.....!";
    memmove(str+20, str+15, 9);
    puts(str);

    return 0;
}

str);

1.2. 字符串拼接
  • char * strcat ( char * destination, const char * source ); 将 source 指针指向的字符串拼接到 destination 指针指向的数组后,destination 的终止字符将被 source 的第一个字符覆盖,同时拼接后的字符串末尾会添加一个终止字符,不支持内存重叠。
  • char * strncat ( char * destination, const char * source, size_t num ); 将 source 指针指向的前 num 个字符串拼接到 destination 指针指向的数组,不支持内存重叠。如果 source 字符串长度小于 num,那么只拼接终止字符 '0' 前面的内容。
1.3. 字符串比较
  • int strcmp ( const char * str1, const char * str2 ); 比较两个字符串 str1 和 str2,从第一个字符开始比较,如果相同则继续往下比较,直到遇到不同字符或者终止字符结束比较。
  • int strncmp ( const char * str1, const char * str2, size_t num ); 和 strcmp 类似,只不过是直到遇到不同字符或者终止字符或者已经比较了 num 个字符串就结束比较。
  • int memcmp ( const void * ptr1, const void * ptr2, size_t num ); 和 strcmp 类似,只不过是遇到终止字符不会结束比较
  • int strcoll ( const char * str1, const char * str2 ); 由当前所选 C 语言环境的 LC_COLLATE 类别来定义比较规则,比如汉字可以按照拼音来比较。
  • size_t strxfrm ( char * destination, const char * source, size_t num ); 根据当前所选 C 语言环境的 LC_COLLATE 类别来转换 source 指向的字符串,并将转换后字符串的前 num 个字符复制到 destination,返回其长度。如果指定 destination 为空指针,num 为 0,此函数可以返回字符串的长度。
1.4. 字符串搜索
  • const void * memchr ( const void * ptr, int value, size_t num ); 查找 ptr 指向内存的前 num 个字节,看是否存在 value,如果存在则返回指向它的指针,value 和 ptr 指向的数组都被转化为无符号字符型数据来进行比较。
  • const char * strchr ( const char * str, int character ); 查找 str 指向的字符数组中是否存在 character,如果存在则返回第一个指向它的指针,character 被转化为字符型数据来进行比较,终止字符也可以被定位
  • const char * strrchr ( const char * str, int character ); 与 strchr 类似,只不过是返回最后一个指向它的指针
  • size_t strcspn ( const char * str1, const char * str2 ); 扫描在字符串 str1 中出现的第一个任意 str2 中的字符,返回找到时已经从 str1 中读取的字符个数,包括终止字符的搜索,也就是说,若 str2 中没有字符存在于str1 中,则返回 str1 的长度。
  • const char * strpbrk ( const char * str1, const char * str2 ); 扫描在字符串 str1 中出现的第一个任意 str2 中的字符,返回指向那个字符的指针,不包括终止字符的搜索但会在终止字符处结束,也就是说,若 str2 中没有字符存在于str1 中,则返回空指针。
  • size_t strspn ( const char * str1, const char * str2 ); 返回字符串 str1 中只包含 str2 中字符的部分的长度。如果 str1 中第一个字符就不在 str2 中,则返回 0;如果 str1 中所有字符都在 str2 中,则返回 str1 的长度;
  • const char * strstr ( const char * str1, const char * str2 ); 扫描字符串 str2 在 str1 中第一次出现的位置,不包括终止字符的搜索但会在终止字符处结束。
  • char * strtok ( char * str, const char * delimiters ); 在字符串 str 中先找到第一个不在 delimiters 中的字符,也就是口令的起始位置,然后以此为起点再继续找到第一个位于 delimiters 中的字符,这是口令的结束位置。然后通过传入一个 NULL 指针和新的 delimiters,函数会继续从上一个口令结束位置的下一个字符继续查找。
/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string "%s"" into tokens:n""

脚本宝典总结

以上是脚本宝典为你收集整理的str);全部内容,希望文章能够帮你解决str);所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。