首页 > 其他分享 >chapter4-字符串

chapter4-字符串

时间:2024-02-20 13:23:21浏览次数:32  
标签:字符 下标 string chapter4 str 字符串 include

记录字符串常考的4种操作:遍历、加密、统计、匹配

1.字符串介绍

C++提供了字符串(string)这种基本数据类型,它可以很方便地对字符串进行各种操作。使用需要添加头文件#include<string>

1.1字符串的构造

字符串的构造包括定义和初始化两个部分,定义一个字符串的方式和定义其他基本数据类型的方式相同,如string str;,此外还有很多种常用的定义方式,可根据题面需要进行选择。

字符串定义
//常见的定义字符串的一些方法  2024-02-17
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string s0 = "Initial String"; //初始化可以直接给字符串变量赋值
    string s1;
    string s2 (s0); //把s0的拷贝给s2
    string s3 (s0, 8, 3);  //从字符串s0的下标8开始取3个字符拷贝给s3(下标从0开始) Str
    string s4 ("A character sequence"); //用构造函数的方式给s4值
    string s5 ("Another character sequence", 12); //Another char 只需要前面12个字符
    string s6 (10, 'x');   //假如你现在要建立一个字符串,这个字符串由n个重复的字符构成,使用这种方式 10个x

    cout << "s0:" << s0 <<endl;
    cout << "s1:" << s1 <<endl;
    cout << "s2:" << s2 <<endl;
    cout << "s3:" << s3 <<endl;
    cout << "s4:" << s4 <<endl;
    cout << "s5:" << s5 <<endl;
    cout << "s6:" << s6 <<endl;
    return 0;
}

1.2字符串的操作

1、对于操作来说,最重要的就是访问,要访问到字符串的每一位字符。字符串类型已经重载了[],让我们能够像访问数组元素一样用下标访问字符串的每一位。

2、第二种操作是插入,可以在字符串的任意位置插入一段字符串或者插入字符。调用str4.insert(pos1,str, pos2, n)函数,其中str4是我们定义的变量名字,有4个参数,第一个参数指明从字符串str4的哪个下标位置开始插入;第二个参数指明插入的字符串内容,可以是字符串变量str2,也可以直接显示给出;第三个参数指明从str2的哪个下标位置开始取字符;第四个参数配合前一个参数指明取字符的个数

3、移除操作。只有两种方式,1:从字符串的某个下标位置开始截断,后面都不要了。2:只删一小部分,第一个参数还是指明从哪个开始删;第二个参数指明一共删除几个字符。str4.erase(pos, n);

4、清空操作。直接调用str4.clear(),清空字符串。

字符串的操作
//字符串的元素操作 2024-02-17
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string str = "hello world";
    for(int i = 0; i < str.size(); ++i) {
        cout << str[i];
    }
    cout << endl << "the 7th element of str is: " << str[6] << endl;

    string str1 = "to be question";
    string str2 = "that is a ";
    string str3 = "or not world";
    string str4;

    str4.insert(0, str1);  //在str4下标为0的位置插入str1   //to be question
    str4.insert(6, str3, 0, 7); //在str4下标为6的位置插入,只插入str3的一部分,从0的位置开始,插7个字符 //to be (or not )question 
    str4.insert(13, "to be ");  //也可以显示的、把要插入的字符串插入进来//to be or not (to be )question 
    str4.insert(19, str2);  //to be or not to be (that is a )question
    cout << "str4: " << str4 << endl;

    str4.erase(18); //从下标哪个位置开始删除
    str4.erase(0, 9);
    cout << "str4: " << str4 << endl;

    str4.clear();
    cout << "str4: " << str4 << endl;
    
    return 0;
}

1.3字符串的运算

字符串的运算与数组不同,字符串的加法变成了字符串的拼接;而> < = != >= <=这一类的运算符也被重载,用字典排序进行大小比较(原本字符串是没有大小之分的)。

字符串的运算符
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string str1 = "to be ";
    string str2 = "nor not to be ";
    string str3 = "that is a question";
    string str = str1 + ",";
    str = str + str2 + ";";
    str += str3;
    cout << str << endl;

    str1 = "ab";
    str2 = "abc";
    str3 = "bcc";
    cout << (str1 <= str2) << endl;
    cout << (str2 != str3) << endl;
    cout << (str3 < str1) << endl;
    return 0;
}

1.4字符串的常用函数

1、返回字符串长度的函数:str1.size()

2、在字符串里查找一小段字符串或者字符:str1.find("xxx"),会返回第一个等于这个小字符串的位置下标。或者可以指定从哪个位置开始找,默认从字符串的首部开始找。如果找得到就返回下标,找不到就返回-1

3、返回字符串的子串的函数:str1.substr(pos, n);有两种方式,1:给定一个下标,从这个下标开始后面所有的字符一并返回给你;2:只要某一小段,给定参数1从哪里开始,参数2总共截取几个字符。

字符串的运算符
//字符串的常用函数 2024-02-17
#include <iostream>
#include <cstdio>
#include <string>

using namespace std;

int main()
{
    string str1 = "bella, Things will just be fine"; 
    cout << str1.size() << endl; //字符个数

    int position1 = str1.find("be");
    int position2 = str1.find("be", 2);
    cout << "position1: " << position1 << endl;
    cout << "position2: " << position2 << endl;

    //也可以找单个字符
    position1 = str1.find('l');
    position2 = str1.find('l', 5);
    cout << "position1: " << position1 << endl;
    cout << "position2: " << position2 << endl;
    
    string str2 = str1.substr(7); //从下标为7的位置开始取子串
    cout << "str2: " << str2 << endl;

    string str3 = str1.substr(7,6); //Things
    cout << "str3: " << str3 << endl;

    return 0;
}

2.字符串操作

2.1 字符串-遍历

标签:字符,下标,string,chapter4,str,字符串,include
From: https://www.cnblogs.com/paopaotangzu/p/18017145

相关文章

  • 统计字符串中出现次数最少的字符
    functiongetMinStr(str){constcountObj={}for(letiofstr){Reflect.has(countObj,i)?countObj[i]++:countObj[i]=1}constresult=Object.entries(countObj).reduce((pre,cur)=>cur[1]>pre[1]?cur:pre)ret......
  • Map判空 、空字符串、空key值等各种判断方法
    一、Map本身的判空1.1“==null”不能判断Map的本身是否为null  1.2map.isEmpty()判断为空当map没有向里面put数据的时候,可以利用map自带得方法来进行判断该Map是否里面有值 1.3“==null”与“isEmpty()”最大的区别如果map是一个null存在,那么在利用isEmpty()来判空将......
  • java中file转字符串,字符串输出为文件
     读取文件转为字符串: //输入文件File类型,输出字符串 publicstaticStringfileToString(Filefile){ InputStreamis=null; ByteArrayOutputStreambos=newByteArrayOutputStream(); byte[]buffer=newbyte[1024]; inttemp=-1; try{ is=newFi......
  • linux字符串处理与赋值
    赋值使用$()例如:a=$(echo"helloworld")将命令echo“helloworld”的输出赋值给变量a字符串切片使用cut[选项]...[文件]...-b:以字节为单位进行分割;-c: 以字符为单位进行分割;-d分隔符: 使用指定分隔符代替制表符作为划分符,默认使用制表符;-f:选择指定的段,如果选......
  • 2-Redis十大类型之字符串String
    1.RedisString类型注意:redis中的string类型最大value为512MB1.1同时设置多个键值对MSETk1v1k2v2k3v3...1.2同时获取多个键值MGETk1k2k3...1.3获取指定区间范围内的值GETRANGEkstartend:类似于字符串截取SETRANGEkoffsetvalue:类似于从字符串的指......
  • VC++ 中 CT2A CA2T 两个宏进行字符串转换简单测试
    #include"afxwin.h"#include<iostream>usingnamespacestd;intmain(){CStringcs=_T("西游记");AfxMessageBox(_T("CString:")+cs);//CString转ACSIICT2Aa_str(cs);stringstd_str(a_str);......
  • 2024-02-18-物联网C语言(7-字符串处理函数)
    7.字符串7.1获取字符串的长度函数-strlen头文件:#include<string.h>函数定义:size_tstrlen(constchar*s)参数:s-指定的字符串返回值:当前字符串的长度#include<stdio.h>#include<string.h>intmain(intargc,charconst*argv[]){//使用strlen获取字符......
  • 字符串
    4.代码 由1.中思路+性质(else),可得代码:voidnxt(){n[0]=-1;intk=-1;intj=0;while(j<t.length()){if(k==-1||t[j]==t[k]){......
  • 字符串匹配——哈希算法
    一、算法原理我们不直接比较字符串\(S\)的字串和模式串\(T\)是否相等,而是比较二者的哈希值。设字符串\(S\)的长度为\(l\),字符串\(T\)的长度为\(m\)。取两个互素的常数\(b\)和\(h\)(\(l<b<h\)),设字符串\(C=c_1c_2...c_m\),则哈希函数为:\[H(C)=(c_1b^{m-1}+......
  • 第四章 字符串
    目  录第四章、字符串521.创建字符串对象52用一对单引号或者双引号创建字符串52使用str()函数创建字符串53使用转义字符532.索引与切片56索引56切片573.使用+和*运算符60使用+运算符拼接字符串60使用*运算符重复字符串61使用in运算符614.使用字......