剑指offer(2):字符串

C语言中的字符串

C语言中字符串的存储方式和数组类似,都是连续定长的内存块。字符串数组以结尾,所以会比正常数组多一位,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
", dest);
    printf("after strncpy() :%s
", strncpy(dest, src, 3));
    printf("after strcpy() :%s
", 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|
", dest);

   strncat(dest2, src, 9);
   printf("after strncat(): |%s|
", 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
", strcmp(a, b));
    printf("strcmp(b, a) : %d
", strcmp(b, a));
    printf("strcmp(a, d) : %d
", strcmp(a, d));

    printf("strncmp(b, c, 2) : %d
", strncmp(b, c, 2));
    printf("strncmp(b, c, 3) : %d
", 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
", s);
    printf("%s
", p1);
    printf("%s
", p2);
    
    return 0;
}
/******输出******
012345A7890123456789012345A7890
A7890123456789012345A7890
A7890
****************/

字符串长度

  • unsigned int strlen (char *s),返回字符串长度,不包含,注意其与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
", strlen(str1), sizeof(str1));
    printf("strlen(str2)=%d, sizeof(str2)=%d
", strlen(str2), sizeof(str2));
    return 0;
}
/******输出******
strlen(str1)=53, sizeof(str1)=54
strlen(str2)=53, sizeof(str2)=100
****************/

Python中的字符串

Python中的字符串可以看做list,Python中提供了方便的字符串操作方式。

字符串运算符

  • 使用+可直接进行字符串拼接
  • 使用*可重复输出字符串
  • 使用[i][:]可对字符串索引和切片
  • innot in可用来判断字符(串)之间的包含关系

常用字符串内建函数string.xxx

  • string.find(str, beg=0, end=len(string))
    检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1
  • string.index(str, beg=0, end=len(string))
    跟find()方法一样,只不过如果str不在 string中会报一个异常.
  • string.format()
    格式化字符串
>>> "{:.2f}".format(3.1415926)
`3.14`
  • string.isalnum()
    如果 string 至少有一个字符并且所有字符都是字母或数字则返回 True,否则返回 False
  • string.isalpha()
    如果 string 至少有一个字符并且所有字符都是字母则返回 True,否则返回 False
  • string.isdecimal()
    如果 string 只包含十进制数字则返回 True 否则返回 False.
  • string.isdigit()
    如果 string 只包含数字则返回 True 否则返回 False.
  • string.islower()
    如果 string 中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False
  • string.rstrip()
    删除 string 字符串末尾的空格.
  • string.split()
    默然以空格为界分割字符,括号内可填入自定分割字符,分隔符可以为普通字符,也可以为 等转义字符。
>>> 'carl_will_go'.split('_will_')
['carl', 'go']
  • 'string.join(seq)'
    以string为间隔拼接序列
>>> '_'.join(['a', 'b', 'c'])
'a_b_c'
  • `string.find(str)'
    查找str在string 中位置,有则返回下标,没有返回-1
>>> 'abc'.find('b')
1
原文地址:https://www.cnblogs.com/carlsplace/p/7163618.html