string

创建

创建一个字符串或者字符串数组如下

用 cin 输入,可以读一整串字符直到空格或换行才结束

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
string strs[N];
cin >> s;
for (int i = 0; i < N; i ++ )
{
cin >> strs[i];
}
cout << s << endl;
for (int i = 0; i < N; i ++ )
{
cout << strs[i] << endl;
}
return 0;
}

访问大小

.size() 和 .length() 可以访问字符串的长度

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s.size() << endl;
return 0;
}

开头与结尾

.begin()是开头的地址

.end()是结尾的地址

插入

.push_back() 可以尾插

.insert() 可以精确插入,其中放插入的位置和插入的字符内容

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s << endl;
s.push_back('5');
cout << s << endl;
s.insert(s.begin(), '0');
cout << s << endl;
return 0;
}

删除

.erase(),里面放地址

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s << endl;
s.erase(s.begin());
cout << s << endl;
return 0;
}

查找

find(),里面放字符串或者字符,返回下标数字

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s << endl;
cout << s.find('1') << endl;
return 0;
}

下面是放字符的情况

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s << endl;
cout << s.find("23") << endl;
return 0;
}

截取

substr(), 放两个数,一个是下标,一个是截取的长度

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s;
cin >> s;
cout << s << endl;
s = s.substr(2, 5);
cout << s << endl;
return 0;
}

比较

string会默认按字典序比较,由高位向低位去比较

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s1 = "123456", s2 = "12345";
cout << (s1 > s2) << endl;
s1 = "12345", s2 = "12346";
cout << (s1 > s2) << endl;
return 0;
}

拼接

#include <iostream>
using namespace std; const int N = 9; int main()
{
string s1 = "123456", s2 = "789";
string s3 = s1 + s2;
cout << s3 << endl;
return 0;
}

反转

reverse(开始地址, 结束地址)可以反转内容

string,字符串使用指南的更多相关文章

  1. Java String字符串/==和equals区别,str。toCharAt(),getBytes,indexOf过滤存在字符,trim()/String与StringBuffer多线程安全/StringBuilder单线程—— 14.0

    课程概要 String 字符串 String字符串常用方法 StringBuffer StringBuilder String字符串: 1.实例化String对象 直接赋值  String str=& ...

  2. [CareerCup] 1.3 Permutation String 字符串的排列

    1.3 Given two strings, write a method to decide if one is a permutation of the other. 这道题给定我们两个字符串,让 ...

  3. 03-Java String字符串详解

    1.Java字符串String A.实例化String字符串:直接赋值(更合理一些,使用较多).使用关键字new. B.String内容的比较 // TODO Auto-generated metho ...

  4. C++学习38 string字符串的增删改查

    C++ 提供的 string 类包含了若干实用的成员函数,大大方便了字符串的增加.删除.更改.查询等操作. 插入字符串 insert() 函数可以在 string 字符串中指定的位置插入另一个字符串, ...

  5. C++学习37 string字符串的访问和拼接

    访问字符串中的字符 string 字符串也可以像字符串数组一样按照下标来访问其中的每一个字符.string 字符串的起始下标仍是从 0 开始.请看下面的代码: #include <iostrea ...

  6. java String字符串——进度1

    String字符串    在JAVA中提供了多种创建字符串对象的方法,这里介绍最简单的两种,    第一种是直接赋值,    第二种是使用String类的构造方法:    如下所示:    Strin ...

  7. 关于String字符串反转

    这是网上看到的一篇java面试题中的问题: 问题是: 如何将一个String字符串反转. String str = "1234567"; int length = str.leng ...

  8. JavaScript的内置对象(Date日期+string字符串)基础语法总结

    1.Date日期对象可以储存任意一个日期,并且可以精确到毫秒数(1/1000 秒). 1)定义一个时间对象 : var Udate=new Date(); //注意:使用关键字new,Date()的首 ...

  9. 【转】String字符串相加的问题

    String字符串相加的问题 前几天同事跟我说我之前写的代码中在操作字符串时候,使用字符串相加的方式而不是使用StringBuffer或者StringBuilder导致内存开销很大.这个问题一直在困扰 ...

  10. 从零开始学习前端JAVASCRIPT — 3、JavaScript基础string字符串介绍

    1:字符串 JS中的任何数据类型都可以当作对象来看.所以string既是基本数据类型,又是对象. 2:声明字符串 基本数据类型:var sStr = '字符串'; 对象的方法:var oStr = n ...

随机推荐

  1. LaTeX 编译警告:Script 'CJK' not explicitly supported within font 'FandolSong-Regular'. Check the typeset output, and if it is okay then ignore this warning. Otherwise a different font should be chosen.

    在编译一篇中文文档时遇到如下警告: Package fontspec Warning: Script 'CJK' not explicitly supported within font 'Fando ...

  2. love music

    https://music.163.com/outchain/player?type=2&id=215239

  3. Centos8下搭建私人开源网盘NextCloud步骤及使用(基于LAMP)

    简介:Nextcloud是一款开源免费的私有云存储网盘项目,可以让你快速便捷地搭建一套属于自己或团队的云同步网盘,从而实现跨平台跨设备文件同步.共享.版本控制.团队协作等功能.它的客户端覆盖了Wind ...

  4. 忘记 mysql 8.0 root 密码 怎么修改

    本文copy自 Centos7重置Mysql 8.0.1 root 密码 问题产生背景: 安装完 最新版的 mysql8.0.1后忘记了密码,向重置root密码:找了网上好多资料都不尽相同,根据自己的 ...

  5. Bit, Byte, ASCII, Unicode, UTF, Base64

    前言 做项目偶尔会接触到 stream 这个感念,不管是 memory stream 还是 file stream,它们又会提到 bytes. 还有像 Identity – 安全基础知识 中提到的 S ...

  6. SpringBoot系列:使用原生JDBC实现对数据库的增删改查

    application.yml spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306 ...

  7. JavaScript习题之判断题

    1. JavaScript是Java语言的脚本形式.( ) 2. JavaScript中的方法名不区分大小写.( ) 3. JavaScript语句结束时的分号可以省略.( ) 4. 通过外链式引入J ...

  8. 对 LLM 工具使用进行统一

    我们为 LLM 确立了一个跨模型的 统一工具调用 API.有了它,你就可以在不同的模型上使用相同的代码,在 Mistral.Cohere.NousResearch 或 Llama 等模型间自由切换,而 ...

  9. 暑假集训CSP提高模拟 16

    \[暑假集训CSP提高模拟 \lim_{x\rightarrow\infty}\frac{8f_{x}}{f_{x+1}}\times(\sqrt{5}+1),\ \forall f_{x}=f_{x ...

  10. Windows 缺失Qt5.xxxx.dll,无法继续执行代码

    事件起因: 客户自行安装完Autodesk系软件后, 软件一直弹窗报错 AutodDesktopApp.exe - 系统错误 Windows软件报错:由于找不到Qt5.xxxx.dll,无法继续执行代 ...