c语⾔字符串中的字符⽆效,字符串操作
字符串主要⽤于编程,字符串在存储上类似字符数组,所以它每⼀位的单个元素都是可以提取的。字符串也有很多操作,在正⽂将对C语⾔、C++和java中对其操作进⾏介绍。
中⽂名
字符串操作
外⽂名
string operations
性    质
对字符串进⾏的操作特    点
类似字符数组c++string类型
释    义
应⽤学科
计算机编程语⾔中使⽤
字符串操作名词介绍
编辑
字符串或串(String)是由数字、字母、下划线组成的⼀串字符。它是编程语⾔中表⽰⽂本的数据类型。在程序设计中,字符串为符号或数值的⼀个连续序列。字符串操作就是以串的整体作为操作对象,如:在串中查某个⼦串、求取⼀个⼦串、在串的某个位置上插⼊⼀个⼦串以及删除⼀个⼦串等。
对于字符串的操作⽅法,在这⾥通过介绍C语⾔、C++和java这三种常⽤的语⾔来说明。
字符串操作应⽤
编辑
C语⾔中字符串操作
1、stpcpy()
功能:拷贝⼀个字符串到另⼀个。
⽤法: char *stpcpy(char *destin, char *source);
举例如下:
#include
#include
int main(void)  {
char string[10];
char *str1 = "abcdefghi";
stpcpy(string, str1);
printf("%s\n", string);
return 0;
2、strcat ()
功能: 字符串拼接函数。
⽤法:char *strcat(char *destin, char *source);
举例如下:
#include
#include
int main(void)  {
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland";
strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c);
printf("%s\n", destination);
return 0;
}
3、strchr ()
功能: 在⼀个串中查给定字符的第⼀个匹配之处。
⽤法::char *strchr(char *str, char c);
举例如下:
#include
#include
int main(void)  {
char string[15];
char *ptr, c = 'r';
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string); else
printf("The character was not found\n");
return 0;
}
4、 strcmp()
⽤法:int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值 > 0;两串相等,返回0。举例如下:
#include
#include
int main(void)  {
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr;
ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n");
ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n");
return 0;
}
5、strcpy()
功能:串拷贝。
⽤法:char *strcpy(char *str1, char *str2);
举例如下:
#include
#include
int main(void)  {
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%s\n", string);
return 0;
}
功能:返回指向错误信息字符串的指针。
⽤法::char *strerror(int errnum);
举例如下:
#include
#include
int main(void)  {
char *buffer;
buffer = strerror(errno);
printf("Error: %s\n", buffer);
return 0;
}
7、strnset()
功能: 将⼀个串中的所有字符都设为指定字符。
⽤法:char *strnset(char *str, char ch, unsigned n);
举例如下:
#include
#include
int main(void)  {
char *string = "abcdefghijklmnopqrstuvwxyz";
char letter = 'x';
printf("string before strnset: %s\n", string);
strnset(string, letter, 13);
printf("string after  strnset: %s\n", string);
return 0;
}
8、strpbrk()
功能:在串中查给定字符集中的字符。
⽤法:char *strpbrk(char *str1, char *str2);
举例如下:
#include  #include
int main(void)  {    char *string1 = "abcdefghijklmnopqrstuvwxyz";    char *string2 = "onm";    char *ptr;
ptr = strpbrk(string1, string2);
if (ptr)        printf("strpbrk found first character: %c\n", *ptr);    else        printf("strpbrk didn't find character in set\n");
9、strrev()
功能:串倒转。
⽤法:char *strrev(char *str);
举例如下:
#include
#include
int main(void)  {
char *forward = "string";
printf("Before strrev(): %s\n", forward);
strrev(forward);
printf("After strrev():  %s\n", forward);
return 0;
}
10、strtod ()
功能:将字符串转换为double型值。
⽤法:double strtod(char *str, char **endptr);
举例如下:
#include
#include
int main(void)  {
char input[80], *endptr;
double value;
printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value); return 0;
}
11、 strtol()
功能:将串转换为长整数。
⽤法:long strtol(char *str, char **endptr, int base);
举例如下:
#include

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。