zoukankan      html  css  js  c++  java
  • String VS Cstring(字符串)

      #include<string> 与 #include<string.h> 这是两个完全不同的头文件,前者用于C++,后者用于C,一般把这两个头文件都包括进去。 越来越觉得需要对 string 中的一些函数的使用进行一次总结了,这几天碰到了好多关于字符串处理的问题,下面介绍一下 string 中常用的函数;

    一、string 类对象的初始化

      string s("Hello !") ;

      string s = "Hell0 !" ;

      string s(10,'a') ;    // 字符串  s 为 : aaaaaaaaaa 10个a

      string s , s = 'a' ;    //将字符 a 赋值给字符串 s ,相当于 s = "a" 

    二、string 中的 strlen 和 length 函数

      string s1 ;

      char s2[100] ;

      strlen(s2) ;  // 返回字符串 s2 的长度 ;

      s1.length() ;  //返回字符串 s1 的长度 ,相当于 strlen(s1.c_str()) ;

    三、string 中的 find 、find_first_of、find_last_of、find_first_not_of、find_last_not_of  函数

      string s("Hello World !") ;

      s.find("o") ;   //  返回的的 o 所在的下标 ;

      s.find("o",5) ;  // 从下标为 5 开始进行查找 ; 

      s.append(s , s.find("o") , 3) ;  // 在字符串 s 后面追加 一个字符串("o W") ;

      s.find_fist_of("abc") ;   //返回字符串abc中任意一个字符最先出现的位置

      s.find_last_of("abc") ;  // 返回字符串abc中任意一个字符最后出现的位置

      s.find_first_not_of("acd") ;   //从字符串 s 中进行查找(从前往后),第一个不在字符串 acd 中的位置

      s.find_last_not_of("acd") ;  //从字符串 s 中进行查找(从后往前),第一个不在字符串 acd 中的位置

    四、string 中的 erase 函数

      string s("Hello World !") ;

      s.erase(5) ;  // 相当于保留当前长度为 5 ,也相当于s[5] = '' ;

    五、string 中的 replace 函数

      string s("Hello World !") ;

      s.replace(2,3,"haha") ;  // 从下标为 2 开始 ,将连续的三个字符替换成 字符串 haha ;

      s.replace(2,3,"haha",1,2) ;  从下标为 2 开始 ,将连续的三个字符替换成 字符串 haha 中的 下标为 1 开始的 连续两个字符

    六、string 中的 insert 、at 函数

      string s("Hello World !") ;

      s.insert(1 , s ) ;     //在下标为 1 的后面再将 s 插入在其后

      s.insert(1 , s , 1 , 3) ;   // 在下标为 1 的后面将 s 的下标为 1 的连续 3 个字符 插入到 后面

      s.at(1) ;  // 返回下标为 1 所对应的元素,相当于s[1] ;

    七、string 中的 c_str 函数

      string s("Hello World !") ;

      printf("%s ",s.c_str()) ;  //返回传统的 char * 类型的字符串,该字符串以 ‘’ 结尾 ;

    八、字符数组 中的 strlen、strcat、strcmp 函数

      strlen    返回字符数组的长度

      strcat  连接两个字符数组

      strcmp  比较两个字符数组

    九、字符数组中的 strchr 、strstr 函数

      char s[] = {"Hello World !"} ;

      strchr(s , 'o') ;   // 返回字符 o 在字符数组 s 中的位置指针

      strstr(s,"ll") ;   //返回字符串 ll 在字符数组 s 中的位置指针

  • 相关阅读:
    云级Keyvalue数据库大比较
    如何诊断ORA125XX连接问题
    Oracle Internal Event:10235 Heap Checking诊断事件
    Oracle Database 11g R2 在Oracle Linux 6上获得认证
    一张图帮你搞懂oracle UGA User Global Area的概念
    Oracle Buffer Cache Chain图解
    ORA04036: 实例使用的 PGA 内存超出 PGA_AGGREGATE_LIMIT
    如何诊断ASMLIB故障
    Oracle RDBMS Server 11gR2 Preinstall RPM For Oracle Linux 6
    图解Oracle Buffer Cache LRU List
  • 原文地址:https://www.cnblogs.com/NYNU-ACM/p/4237424.html
Copyright © 2011-2022 走看看