⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c++常用字符串处理函数及使用示例.txt

📁 本文关键词: c++ 字符串 函数 char *strcpy(char *s1, const char *s2) 将字符串s2复制到字符串数组s1中
💻 TXT
字号:

本文关键词: c++ 字符串 函数 
char *strcpy(char *s1, const char *s2) 

将字符串s2复制到字符串数组s1中,返回s1的值

char *strncpy(char *s1, const char *s2, size_t n)    

将字符串s2中最多n个字符复制到字符串数组s1中,返回s1的值

char *strcat(char *s1, const char *s2) 

将字符串s2添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值

char *strncat(char *s1, const char *s2, size_t n) 

将字符串s2中最多n个字符添加到字符串s1的后面。s2的第一个字符重定义s1的null终止符。返回s1的值

int strcmp(const char *s1, const char *s2) 

比较字符串s1和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值

int strncmp(const char *s1, const char *s2, size_t n) 

比较字符串s1中的n个字符和字符串s2。函数在s1等于、小于或大于s2时分别返回0、小于0或者大于0的值

char * strtok(char *s1,const char *s2) 

用一系列strtok调用将s1字符串标记化(将字符串分成各个逻辑组件,如同一行文本中的每个单词),用字符串s2所包含的字符分隔。 首次调用时包含s1为第一个参数,后面调用时继续标记化同一字符串,包含NULL为第一个参数。每次调用时返回当前标记指针。如果函数调用时不再有更多标记,则返回NULL

size_t strlen(const char *s) 

确定字符串长度,返回null终止符之前的字符数

使用示例:

//源代码在Visual c++6.0环境下编译通过

#include <iostream.h>

#include <string.h>

int main()

{

char str1[50] = "Happy birthday to ", str2[] = "coffeehu";

char temp1[100],temp2[6], * temp;

char str[] = "This is a sentence with 7 tokens";

strcpy(temp1, str1);

strncpy(temp2, str1, 5);

temp2[5] = '';

cout << "strcpy result: " <<temp1 << "n";

cout << "strncpy result: " << temp2 << "n";

cout << "strcat result: " << strcat(str1, str2) << "n";

cout << "strncat result: " << strncat(str1, str2, 6) <<"n";

cout << "strcmp result: " << strcmp(temp2,"Happ") <<"n";

cout << "strncmp result: " << strncmp(str1,"Happy",5) <<"n";

//strtok function eg. 

temp = strtok(str, " ");

while(temp != NULL)

{

cout << temp <<'n';

temp = strtok(NULL, " ");

}

cout << "strlen result: " << strlen(str2) <<"n";

return 0;

}


1. UpperCase
  将指定的AnsiString字符串转换为大写形式,函数原型如下:
  AnsiString __fastcall UpperCase(const AnsiString S);

  2. LowerCase
  将指定的AnsiString字符串转换为小写形式,函数原型如下:
  AnsiString __fastcall LowerCase(const AnsiString S);

  3. CompareStr
  比较两个AnsiString字符串,函数原型如下:
  int __fastcall CompareStr(const AnsiString S1, const AnsiString S2);

  4. CompareText
  比较两个AnsiString字符串,函数原型如下:
  int __fastcall CompareText(const AnsiString S1, const AnsiString S2);

  5. StrLen
  返回字符串的长度,函数原型如下:
  Cardinal __fastcall StrLen(const char * Str);

  6. StrEnd
  返回字符串结尾指针,函数原型如下:
  char * __fastcall StrEnd(const char * Str);

  7. StrMove
  从源字符串向目的字符串拷贝指定数目的字符,函数原型如下:
  char * __fastcall StrMove(char * Dest, const char * Source, Cardinal Count);

  8. StrCopy
  将源字符串拷贝到目的字符串中,函数原型如下:
  char * __fastcall StrCopy(char * Dest, const char * Source);

  9. StrECopy
  将源字符串拷贝到目的字符串中,并返回目的字符串结尾指针,函数原型如下:
  char * __fastcall StrECopy(char * Dest, const char * Source);

  10.StrLCopy
  将源字符串指定数目的字符拷贝到目的字符串中,并返回目的字符串指针,函数原型如下:
  char * __fastcall StrLCopy(char * Dest, const char * Source, Cardinal MaxLen);

  11.StrPCopy
  将AnsiString类型的源字符串拷贝到目的字符串中,并返回目的字符串指针,函数原型如下:
  char * __fastcall StrPCopy(char * Dest, const AnsiString Source);

  12.StrPLCopy
  将源字符串(AnsiString类型)指定数目的字符拷贝到目的字符串中,并返回目的字符串
指针,函数原型如下:
  char * __fastcall StrPLCopy(char * Dest, const AnsiString Source, Cardinal MaxLen);

  13.StrCat
  连接两个字符串,并返回目的字符串指针,函数原型如下:
  char * __fastcall StrCat(char * Dest, const char * Source);

  14.StrLCat
  将指定数目的源字符串连接到目的字符串,并返回目的字符串指针,函数原型如下:
  char * __fastcall StrLCat(char * Dest, const char * Source, Cardinal MaxLen);

  15.StrComp
  两个字符串相到比较,返回比较的结果,函数原型如下:
  int __fastcall StrComp(const char * Str1, const char * Str2);

  16.StrIComp
  两个字符串相互比较(不论大小写),返回比较的结果,函数原型如下:
  int __fastcall StrIComp(const char * Str1, const char * Str2);

  17.StrLComp
  对两个字符串指定数目的字符进行比较操作,函数原型如下:
  int __fastcall StrLComp(const char * Str1, const char * Str2, Cardinal MaxLen);

  18.StrScan
  在指定的字符串中寻找特定的字符,并返回字符串中第一个特定字符的指针,函数原型如下:
  char * __fastcall StrScan(const char * Str, char Chr);

  19.StrRScan
  在指定的字符串中寻找特定的字符,并返回字符串中最后一个特定字符的指针,函数原型如下:
  char * __fastcall StrRScan(const char * Str, char Chr);

  20.StrPos
  在Strl所指定的字符串中寻找Str2所指定的子字符串,并返回Str2在Str2中第一个子字符的指针,函数原型如下:
  char * __fastcall StrPos(const char * Str1, const char * Str2);

  21.StrUpper
  将字符串转换为大写形式,函数原型如下:
  char * __fastcall StrUpper(char * Str);

  22.StrLower
  将字符串转换为小写形式,函数原型如下:
  char * __fastcall StrLower(char * Str);

  23.StrPas
  将指定的字符串转换为AnsiString类型字符串对象,函数原型如下:
  AnsiString __fastcall StrPas(const char * Str);

  24.StrAlloc
  为字符串分配指定字节的内存,并返回内存指针,函数原型如下:
  char * __fastcall StrAlloc(Cardinal Size);

  25.StrBufSize
  返回*Str所指向内存的大小,函数原型如下:
  Cardinal __fastcall StrBufSize(const char * Str);

  26.StrNew
  在堆中为指定字符串分配空间,并将字符串拷贝到此空间中,函数原型如下:
  char * __fastcall StrNew(const char * Str);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -