自学内容网 自学内容网

c++ 常用字符串函数

 ## 替换字符串函数:replace()
    replace("要替换的内容",要替换的位置,“替换成的东西”)
    replace("要替换的指定位置",要替换的长度,“替换成的东西”)
    eg:line="this is a ab ,not bad"
       replace(line.find("ab"),1,"")  -- 把line字符串中第一个位置的ab换成空
       line.replace(line.begin(),line.begin()+6,"");  -- 把line从begin开始的6个字符替换成空
       line.replace(0, 5, str); //用str替换从指定位置0开始长度为5的字符串
  ## begin() end()
    # line.begin()    -- 返回字符串的首位字符的下标位置
                      -- 注意:只返回首位,不是返回首个出现的字符?
    # line.end()   -- 返回字符串中的末尾的下标位置+1,即下标位置多了一个位置
                   -- 若想得到末尾字符的下标位置,需要end()-1
  ## 插入 insert(索引,“插入的数据”)
     string site = "hello";
     site.insert(2,"L")   -- 插入索引为2的位置,输出heLllo
  ## 两个字符串进行比较  str1.compare(str2)
  ## 交换字符串 swap(str1, str2)
  ## str1.size()
  ## str1.length()

原文地址:https://blog.csdn.net/xzal12/article/details/142915577

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!