C++ 字符串的一些操作

1.
字符串长度查询: strlen() 和 wcslen()

Finding the Length of a Null - Terminated String:

The strlen() function returns the length of the argument string of type char* as a value of type size_t .
The wcslen() function does the same thing for strings of type wchar_t* .

char * str("A miss is as good as a mile.");
cout < < "The string contains " < < strlen(str) < < " characters." < < endl;


Both strlen() and wcslen() fi nd the length by looking for the null at the end. 
If there isn ’ t one, the functions will happily continue beyond the end of the string, checking throughout memory in the hope of fi nding a null.

For this reason, these functions represent a security risk when you are working with data from an untrusted external source。

In this situation you can use the strnlen() and wcsnlen() functions, both of which require a second argument that specifi es the length of the buffer in which the string specifi ed by the fi rst argument is stored.


2. 字符串连接:
Joining Null - Terminated Strings:

The strcat() function concatenates two null - terminated strings.

char str1[30]= "Many hands";
char* str2(" make light work.");
strcat(str1, str2);
cout < < str1 < < endl;

The string specifi ed by the fi rst argument must have suffi cient space to accommodate the two strings when they are joined. If it doesn ’ t, disaster will surely result because the function will then try to overwrite the area beyond the end of the first string.

The strcat() function returns the pointer that is the first argument, so you could combine the last two statements in the fragment above into one:
cout < < strcat(str1, str2) < < endl;

The wcscat() function concatenates wide - character strings, but otherwise works exactly the sameas the strcat() function.
The wcsncat() provides the same capability as strncat() butfor wide - character strings.

With the strncat() function you can append part of one null - terminated string to another. The first two arguments are the destination and source strings respectively, and the third argument is a count of the number of characters from the source string that are to be appended。

The strcat_s() , wcscat_s() , strncat_s() , and wcsncat_s() functions in < cstring > provide secure alternatives.

const size_t count = 30;
char str1[count]= "Many hands";
char* str2(" make light work.");
errno_t error = strcat_s(str1, count, str2);

if(error == 0)
    cout < < " Strings joined successfully." < < endl;
else if(error == EINVAL)
    cout < < "Error! Source or destination string is NULL." < < endl;
else if(error == ERANGE)
    cout < < " Error! Destination string too small." < < endl;

EINVAL if the source or destination is NULLPTR , or ERANGE if the destination length is too small.



3. 字符串拷贝
Copying Null - Terminated Strings
The standard library function strcpy() copies a string from a source location to a destination。

const size_t LENGTH = 22;
const char source[LENGTH] ="The more the merrier!";
char destination[LENGTH];
cout < < "The destination string is: " < < strcpy(destination, source)
< < endl;

You must ensure that the destination string has suffi cient space to accommodate the source string. 
If you don ’ t, something will get overwritten in memory, and disaster is the likely result.

The  strcpy_s() function is a more secure version of strcpy() . It requires an extra argument between the destination and source arguments that specifi es the size of the destination string buffer. The strcpy_s() function returns an integer value of type errno_t that indicates whether an error
occurred.

const size_t LENGTH(22);
const char source[LENGTH] ="The more the merrier!";
char destination[LENGTH];
errno_t error = strcpy_s(destination, LENGTH, source);

if(error == EINVAL)
    cout < < "Error. The source or the destination is NULLPTR." < < endl;
else if(error == ERANGE)
    cout < < "Error. The destination is too small." < < endl;
else
    cout < < "The destination string is: " < < destination < < endl;

You need to include the cstring and cerrno headers for this to compile.


4.
Comparing Null - Terminated Strings

The strcmp() function compares two null - terminated strings that you specify by arguments that are pointers of type char* .
char* str1("Jill");
char* str2("Jacko");
int result = strcmp(str1, str2);
if(result < 0)
    cout < < str1 < < " is less than " < < str2 < < '.' < < endl;
else if(0 == result)
    cout < < str1 < < " is equal to " < < str2 < < '.' < < endl;
else
    cout < < str1 < < " is greater than " < < str2 < < '.' < < endl;

The wcscmp() function is the wide - character string equivalent of strcmp() .


5.
Searching Null - Terminated Strings

The strspn() function searches a string for the fi rst character that is not contained in a given set and returns the index of the character found.

char* str = "I agree with everything.";
char* vowels = "aeiouAEIOU ";
size_t index = strspn(str, vowels);
cout < < "The first character that is not a vowel is '" < < str[index]< < "' at position " < < index < < endl;



The strstr() function returns a pointer to the position in the fi rst argument of a substring specifi ed by the second argument.

char* str = "I agree with everything.";
char* substring = "ever";
char* psubstr = strstr(str, substring);
if(!psubstr)
cout < < "\"" < < substring < < "\" not found in \"" < < str < < "\"" < < endl;
else
cout < < "The first occurrence of \"" < < substring
< < "\" in \"" < < str < < "\" is at position "
< < psubstr-str < < endl;



6.
int num =atoi(str);
 
 
double atof(
   const char *string 
);
double _wtof(
   const wchar_t *string 
);
int atoi(
   const char *string 
);
__int64 _atoi64(
   const char *string 
);
int _wtoi(
   const wchar_t *string 
);
__int64 _wtoi64(
   const wchar_t *string 
);
long atol(
   const char *string 
);
long _wtol(
   const wchar_t *string 
);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值