标签:大小 sed list asc sig 复制 字符串拼接 gre 运算符
C语言中字符串的存储方式和数组类似,都是连续定长的内存块。字符串数组以\0
结尾,所以会比正常数组多一位,char str3[5] = "1234"; //此处赋值字符串长度应小于5
。
<string.h>
char *strcpy(char *dest, const char *src)
,将字符串src复制给dest。char *strncpy(char *dest, const char *src, int n)
,将字符串src的前n个字符复制给dest。
#include <stdio.h>
#include <string.h>
int main()
{
char dest[30] = "hello world";
char src[] = "carlsplace";
printf("original :%s\n", dest);
printf("after strncpy() :%s\n", strncpy(dest, src, 3));
printf("after strcpy() :%s\n", strcpy(dest, src));
return 0;
}
/******输出******
original :hello world
after strncpy() :carlo world
after strcpy() :carlsplace
****************/
char *strcat(char *dest, const char *src)
,将字符串src拼接到dest后面char *strncat(char *dest, const char *src, int n)
将字符串src的前n个字符拼接到dest后面
int main ()
{
char src[50], dest[50], dest2[50];
strcpy(src, "This is source.");
strcpy(dest, "This is destination1.");
strcpy(dest2, "This is destination2.");
strcat(dest, src);
printf("after strcat(): |%s|\n", dest);
strncat(dest2, src, 9);
printf("after strncat(): |%s|\n", dest2);
return(0);
}
/******输出******
after strcat(): |This is destination1.This is source.|
after strncat(): |This is destination2.This is s|
****************/
int strcmp(const char *s1, const char *s2);
,比较字符串s1和s2,返回s1-s2,相等返回0。strcmp() 以二进制的方式进行比较,不会考虑多字节或宽字节字符int strncmp(const char *s1, const char *s2, int n);
,比较字符串s1的和s2的前n个字符
#include <stdio.h>
#include <string.h>
int main()
{
char a[] = "aBcDeF";
char b[] = "aaCdEf";
char c[] = "aacdef";
char d[] = "aBcDeF";
printf("strcmp(a, b) : %d\n", strcmp(a, b));
printf("strcmp(b, a) : %d\n", strcmp(b, a));
printf("strcmp(a, d) : %d\n", strcmp(a, d));
printf("strncmp(b, c, 2) : %d\n", strncmp(b, c, 2));
printf("strncmp(b, c, 3) : %d\n", strncmp(b, c, 3));
return 0;
}
/******输出******
strcmp(a, b) : -31
strcmp(b, a) : 31
strcmp(a, d) : 0
strncmp(b, c, 2) : 0
strncmp(b, c, 3) : -32
****************/
char * strchr (const char *str, int c)
,其中参数c
须传入字符,会自动被转化为ASCII码。函数返回字符c
第一次出现位置的指针char *strrchr(const char *str, int c)
,函数返回字符c
最后一次出现位置的指针
#include <stdio.h>
#include <string.h>
int main(){
char s[] = "012345A7890123456789012345A7890";
char *p1 = NULL;
char *p2 = NULL;
p1 = strchr(s, ‘A‘);
p2 = strrchr(s, ‘A‘);
printf("%s\n", s);
printf("%s\n", p1);
printf("%s\n", p2);
return 0;
}
/******输出******
012345A7890123456789012345A7890
A7890123456789012345A7890
A7890
****************/
unsigned int strlen (char *s)
,返回字符串长度,不包含\0
,注意其与sizeof()
的区别
#include<stdio.h>
#include<string.h>
int main()
{
char str1[] = "The Arch-based Manjaro is a great Linux distribution.";
char str2[100] = "The Arch-based Manjaro is a great Linux distribution.";
printf("strlen(str1)=%d, sizeof(str1)=%d\n", strlen(str1), sizeof(str1));
printf("strlen(str2)=%d, sizeof(str2)=%d\n", strlen(str2), sizeof(str2));
return 0;
}
/******输出******
strlen(str1)=53, sizeof(str1)=54
strlen(str2)=53, sizeof(str2)=100
****************/
Python中的字符串可以看做list,Python中提供了方便的字符串操作方式。
+
可直接进行字符串拼接*
可重复输出字符串[i]
和[:]
可对字符串索引和切片in
和not in
可用来判断字符(串)之间的包含关系string.xxx
string.find(str, beg=0, end=len(string))
string.index(str, beg=0, end=len(string))
string.format()
>>> "{:.2f}".format(3.1415926)
`3.14`
string.isalnum()
string.isalpha()
string.isdecimal()
string.isdigit()
string.islower()
string.rstrip()
string.split()
\n
等转义字符。
>>> ‘carl_will_go‘.split(‘_will_‘)
[‘carl‘, ‘go‘]
>>> ‘_‘.join([‘a‘, ‘b‘, ‘c‘])
‘a_b_c‘
>>> ‘abc‘.find(‘b‘)
1
标签:大小 sed list asc sig 复制 字符串拼接 gre 运算符
原文地址:http://www.cnblogs.com/carlsplace/p/6850536.html