String Manipulation functions allow you to manipulate or compare strings. These functions begin with the str prefix. Expand the category to view a list of the available functions.
Function Name | Description |
strcat | Concatenates two strings. |
strchr | Returns the pointer to the first occurrence of a character in a string. |
strcmp | Compares two strings to determine the alphabetic order. |
strcpy | Copies one string to another. |
strdup | Duplicates a string. |
stricmp | Performs a case-insensitive comparison of two strings. |
strlen | Returns the length of a string. |
strlwr | Converts a string to lower case. |
strncat | Concatenates n characters from one string to another. |
strncmp | Compares the first n characters of two strings. |
strncpy | Copies the first n characters of one string to another. |
strnicmp | Performs a case-insensitive comparison of n strings. |
strrchr | Finds the last occurrence of a character in a string. |
strset | Fills a string with a specific character. |
strspn | Returns the length of the leading characters in a string that are contained in a specified string. |
strstr | Returns the first occurrence of one string in another. |
strtok | Returns a token from a string delimited by specified characters. |
strupr | Converts a string to upper case. |
strdup //字符串复制
strlwr //转换为小写字符
strupr //转换为大写字符
strncat//连接n个字符到另一字符串
strcat
//char *strcat(char *to,const char *from);
char fullpath[1024],*filename="logfile.txt";
strcpy(fullpath,"c:\\tmp\\");//字符串复制
strcat(fullpath,filename);//字符串连接
lr_output_message("Full path of file is %s",fullpath);
strchr
strrchr
char *string ="His Excellency the Duke of Exeter";
char *first_x,*last_x;
//获取搜索字符第一次出现的位置以后的字符串,如:xcellency the Duke of Exeter
first_x=(char *)strchr(string,'x');
lr_output_message("The first occurrence of x: %s",first_x);
//获取搜索字符最后一次出现的位置以后的字符串,如:xeter
last_x=(char *)strrchr(string,'x');
lr_output_message("The last occurrence of x: %s",last_x);
strlen
int is_digit, i = 0;
char * str = "1234567k89";
unsigned int len = strlen(str);
do {
if (i == len) {
lr_output_message ("No letters found in string");
return -1;
}
//isdigit函数,返回十进制数非0(true),返回0(false)
is_digit = isdigit(str[i++]);
}
//当为0是循环条件终止
while (is_digit);
lr_output_message ("The first letter appears in character %d of string", i);}
strcmp
stricmp
strncmp
strnicmp
int result;
char tmp[20];
char string1[]="The quick brown dog jumps over the lazy fox";
char string2[]="The QUICK brown dog jumps over the lazy fox";
//大小写敏感(小写>大写)
result = strcmp(string1,string2);
if(result>0)
strcpy(tmp, "greater than");
else if(result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal to");
lr_output_message("strcmp:String1 is %s string2",tmp);
//大小写不敏感(小写=大写)
result = stricmp(string1,string2);
if( result > 0 )
strcpy( tmp, "greater than" );
else if( result < 0 )
strcpy( tmp, "less than" );
else
strcpy( tmp, "equal to" );
lr_output_message( "stricmp: String 1 is %s string 2", tmp);
//比较string1和string2前30个字符,大小写敏感
result = strncmp( string1, string2 , 30);
if (result > 0 )
strcpy(tmp, "greater than");
else if (result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal to");
lr_output_message ("strncmp: String 1 is %s string 2", tmp);
//比较string1和string2前30个字符,大小写不敏感
result = strnicmp( string1, string2 , 30);
if (result > 0 )
strcpy( tmp, "greater than");
else if (result < 0)
strcpy(tmp, "less than");
else
strcpy(tmp, "equal to");
lr_output_message ("strnicmp: String 1 is %s string 2", tmp);
strset
char str[16];
strcpy(str, "Fill me in");
lr_output_message ("Before strset str=%s", str);
lr_output_message("%d",strlen(str));//字符串长度为10
//用字符'x'填充
strset(str, 'x');
lr_output_message ("After strset str=%s", str);
strspn
char * str = "corroborative";
int rc;
//返回字符串'corroborative'首位是否包含'ro'
if ((rc = strspn(str, "ro")) == 0)
lr_output_message ("No o's or r's found");
else
lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));
//返回字符串'corroborative'首位+1是否包含'ro'
if ((rc = strspn(str + 1, "ro")) == 0)
lr_output_message ("No o's or r's found");
else
lr_output_message ("%d of %d characters are an o or an r", rc, strlen(str));
strstr
int offset;
char * position;
char * str = "The quick brown dog jumps over the lazy fox";
char * search_str = "dog";
//返回搜索字符串'dog'在str中的第一个位置
position = (char *)strstr(str, search_str);
// strstr has returned the address. Now calculate * the offset from the beginning of str
//按照内存中的相对位置计算offset量
offset = (int)(position - str + 1);
lr_output_message ("The string \"%s\" was found at position %d", search_str, offset);
strtok
extern char * strtok(char * string, const char * delimiters ); // Explicit declaration
char path[] = "c:\\mercury\\lrun\\bin\\wlrun.exe";
\\分隔符号为.或\
char separators[] = ".\\";
char * token;
token = (char *)strtok(path, separators); //获取第一个token
if (!token) {
lr_output_message ("No tokens found in string!");
return( -1 );
}
while (token != NULL ) {
//打印有效的token
lr_output_message ("%s", token );
//获取下一个token,后续调用必须传递NULL作为字符串parameter.
token = (char *)strtok(NULL, separators);