字符串

字符串是最常用的一种数据类型了,在python中声明字符串和声明其他类型的数据一样,都非常的简单。但是在c++中,对于字符串的操作,相对来说要稍微复杂一些。

C++ 提供了以下两种类型的字符串表示形式:

  • C 风格字符串

  • C++ 引入的 string 类类型

C风格字符串

C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持。字符串实际上是使用 null 字符 \0 终止的一维字符数组。如下面的声明和初始化创建了一个 "Hello" 字符串。由于在数组的末尾存储了空字符,所以字符数组的大小比单词 "Hello" 的字符数多一个。

int main(){

    char greeting[] = {'H', 'e', 'l', 'l', 'o', '\0'};

    //可以简写成:
char greeting2[] = "Hello"; return ;
}

c风格字符串常见操作

1.遍历字符串

#include<iostream>
int main(){ //最后总会跟着一个\0的空字符,此时括号中如果写长度,必须大于等于6
char name[] = "hello"; for (int i = ; i < sizeof(name ) / sizeof(char); ++i) {
std::cout << name[i] << std::endl;
}
}

2.字符串的其他操作

C语言中提供了针对字符串操作的大量函数,不过在使用之前,需要先引入 #include<cstring>

以下函数的使用,需要引入 #include<ctype.h> 头文件

拷贝、拼接字符串

#include <iostream>

int main(){

    //拷贝字符串
char name[] = "hello";
char name2[]; //参数一: 目标字符串, 参数二:源字符串
strcpy(name2 , name);
std::cout << name2 << std::endl; //拼接字符串
strcat(name2 , " , 张三");
std::cout << name2 << std::endl; // 返回字符串长度
int len = strlen(name2);
std::cout << "name2的长度:" << len << std::endl; return ;
}

C++风格字符串

C++ 标准库提供了 string 类类型,支持上述所有的操作,另外还增加了其他更多的功能。需要引入 #include<string> ,由于string类声明在命名空间 std ,所以在使用的首要注意 命名空间的联合使用


#include <iostream>
//必须引入string库
#include <string> using namespace std; int mian(){ string s1;
string s2 {"北京"};
string s3{s2}; string s4 = "你好"; s1 = s3;
return ;
}

C++风格字符串常见操作

1.拼接字符串

c++字符串的拼接跟Python一样,直接用+拼接即可。

#include <iostream>
#include<string>
using namespace std; int main(){
string part1 {"c++"};
string part2 {" is a powerful"};
string sentence ;
sentence = part1 + part2 ;
cout << sentence << endl; // 结果是 c++ is a powerful
return ;
}

2.获取指定位置的字符

可以使用[]和 at()操作字符串

#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"i love c++"}; cout << s1[]<<endl; // o
cout << s1.at() << endl; // i
return ;
}

3.遍历字符串

#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"abcdef"}; for(char s : s1){
cout << s << " "; // 打印的结果 :a b c d e f
}
cout << endl; for(int s : s1){
// 因为s1是字符串类型,指定的s又为int类型,所以这里面打印出来是字符所对应的ascii码
cout << s << " "; // 打印的结果:97 98 99 100 101 102
}
cout << endl;
return ;
}

4.字符串比较

字符串也是可以比较大小的。

#include <iostream>
#include<string>
using namespace std; int main(){ string s1{"Apple"};
string s2{"Banana"};
string s3 {"kiwi"};
string s3 {"apple"};
string s3 {s1}; s1 == s5 // true
s1 == s2 // false
s1 != s2 // true
s1 < s2 // True
s1 > s2 // false
s1 == "Apple" // false return ;
}

5.截取字符串

#include <iostream>
#include<string>
using namespace std; int main(){ substr(开始索引, 截取长度); string s1 {"This is a test"};
cout << s1.substr( , ) ; // This return ;
}

6.获取字符(字符串)在字符串中的索引

#include <iostream>
#include<string>
using namespace std;
int main(){
find(搜索的字符) string s1 {"This is a test"};
cout << s1.find("This") ; // 0
cout << s1.find("is") ; //
cout << s1.find("test") ; // 10 return ;
}

7.获取字符串长度

length() : 返回字符串长度

size():返回字符串长度

#include <iostream>
#include<string>
using namespace std; int main(){
string s1 {"abcd"};
cout << "s1的字符串长度为" << s1.length() << endl; // 4
   cout << "s1的字符串长度为" << s1.size() << endl; // 4
 return 0 ; }

C++中string类的size和length到底有没有区别?

这是c++标准库中的string中length和size的源码

   _NODISCARD size_type length() const noexcept { // return length of sequence
return _Get_data()._Mysize;
} _NODISCARD size_type size() const noexcept { // return length of sequence
return _Get_data()._Mysize;
}

由此可以看出,length()与size()没有区别,都是返回string对象中元素数量,即返回std::distance(begin(),end()) 。length是因为沿用C语言的习惯而保留下来的,string类最初只有length,引入STL之后,为了兼容又加入了size,它是作为STL容器的属性存在的,便于符合STL的接口规则,以便用于STL的算法。

原文链接:https://blog.csdn.net/qq_43152052/article/details/95861329

每天一个表情包,美滋滋

C++ 第二天 字符串的更多相关文章

  1. 剑指Offer:从第一个字符串中删除第二个字符串中出现过的所有字符

    // 从第一个字符串中删除第二个字符串中出现过的所有字符 #include <stdio.h> char* remove_second_from_first( char *first, c ...

  2. 面试35-删除字符串重复字符-删除出现在第二个字符串中的字符-第一个只出现一次的字符-hash表计数

    #include<iostream>#include<algorithm>#include<functional>using namespace std;char ...

  3. Python学习-第二天-字符串和常用数据结构

    Python学习-第二天-字符串和常用数据结构 字符串的基本操作 def main(): str1 = 'hello, world!' # 通过len函数计算字符串的长度 print(len(str1 ...

  4. <CPP学习 第二天> 字符串的输入 及 String类

    今天简单的学习了字符串的输入以及C++的String类. 1.面向行的输入: getline(); getline()函数读取整行,通过回车键输入的换行符来确定输入结尾.要调用这种方法,可以使用cin ...

  5. python基础知识第二篇(字符串)

    基本数据类型 数字                  整形 int                             ---int                            将字符串 ...

  6. python学习第二天--字符串及格式化输出

    # 字符串# 字符串取值:字符串名[索引值] 只能取单个值# 正序访问,从0开始str1 = "hello world"print(str1[3]) # 输出"l&quo ...

  7. 写一个程序可以对两个字符串进行测试,得知第一个字符串是否包含在第二个字符串中。如字符串”PEN”包含在字符串“INDEPENDENT”中。

    package lovo.test; import java.util.Scanner; public class Java { @param args public static void main ...

  8. Windows核心编程第二章,字符串的表示以及宽窄字符的转换

    目录 Windows核心编程,字符串的表示以及宽窄字符的转换 1.字符集 1.1.双字节字符集DBCS 1.2 Unicode字符集 1.3 UTF-8编码 1.4 UTF - 32编码. 1.5 U ...

  9. 【原创】Python第二章——字符串

    字符串是一个字符序列,(提醒:序列是Python的一个重要的关键词),其中存放UNICODE字符.Python中的字符串是不可变的(immutable),即对字符串执行操作时,总是产生一个新的字符串而 ...

  10. SQL Server(第二章) 字符串函数、日期时间函数、转换函数

    --1.CONCAT 函数:字符串连接(支持sql server2012 SQL规则 如果与NULL连接返回NILL) SELECT empid,CONCAT(firstname,lastname) ...

随机推荐

  1. Python3笔记012 - 3.3 条件表达式

    第3章 流程控制语句 3.3 条件表达式 在程序开发中,经常会根据表达式的结果,有条件地进行赋值. # 返回两个数中较大的数 a = 10 b = 6 if a>b: r = a else: r ...

  2. 前端性能优化_css加载会造成哪些阻塞现象?

    css的加载是不会阻塞DOM的解析,但是会阻塞DOM的渲染,会阻塞link后面js语句的执行.这是由于浏览器为了防止html页面的重复渲染而降低性能,所以浏览器只会在加载的时候去解析dom树,然后等在 ...

  3. 洛谷 P3627 [APIO2009]抢掠计划 Tarjan缩点+Spfa求最长路

    题目地址:https://www.luogu.com.cn/problem/P3627 第一次寒假训练的结测题,思路本身不难,但对于我这个码力蒟蒻来说实现难度不小-考试时肛了将近两个半小时才刚肛出来. ...

  4. uni-app中textarea组件

    textarea组件,官方给出的监听事件有以下事件: 其中一定要注意,当使用 v-model 对表单内容进行双向绑定的时候,@input 事件是在绑定变量变化前触发的,所以如果在input事件内打印绑 ...

  5. Django---进阶2

    目录 数据的查,改,删 django orm中如何创建表关系 django请求生命周期流程图(必会) 路由层 路由匹配 无名分组 有名分组 无名有名是否可以混合使用 反向解析 作业 数据的查,改,删 ...

  6. SpringBoot日志功能

    三.SpringBoot日志功能 1.日志框架 市面上的日志框架: JUL.JCL.Jboss-logging.Logback.Log4j.Log4j.SLF4J... 日志门面(日志的抽象层) 日志 ...

  7. 前端08 /jQuery标签操作、事件

    前端08 /jQuery标签操作.事件 目录 前端08 /jQuery标签操作.事件 1.标签内文本操作 1.1 html标签元素中的所有内容 1.2 text 标签元素的文本内容 2.文档标签操作 ...

  8. JVM详解之:运行时常量池

    目录 简介 class文件中的常量池 运行时常量池 静态常量详解 String常量 数字常量 符号引用详解 String Pool字符串常量池 总结 简介 JVM在运行的时候会对class文件进行加载 ...

  9. linux $* 和$@例子

    参见ibm网站示例: https://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html 示例: [ian@pinguino ~]$ ...

  10. OSCP Learning Notes - WebApp Exploitation(4)

    Local File Inclusion[LFI] Target Pentester Lab: Download from the following website: https://www.vul ...