C++ 字符串的概念

什么是字符串

字符串就是0个或多个“字符”组成的“有序”序列。

字符串长度

字符串包含的字符个数(不是指中文)。

空字符串的长度是0

“”是空字符串常量,没有一个字符,长度是0

“ ”是空格字符串常量,包含1个空格,长度是1

“god”  的字符串长度是 3

“我爱你” 的字符串长度不是3 ,是6或9

(在某些编码中,一个汉字占用2个直接,有些编码中占3个字节)

“” 的字符串长度是0(空串)

“ ” 的字符串长度是1(含有一个空格)

字符串常量

“字面型”字符串常量,要求用“”扩起来。

printf("name=%s", "C++");  //C语言方式输出字符串,%s用来匹配字符串

cout << “C++”;   //C++方式输出字符串

字符串结束符

c语言中,为了便于存储字符串,要求在最后一个字符的后面存储一个0(一个字节)。

这个0, 称为“字符串结束符”,常用 ‘\0’ 表示。

“China”  =>   ‘C’   ‘h’   ‘i’    ‘n’   ‘a’    ‘\0’

“”       =>   ‘\0’

 字符串变量的表示

在C语言中,使用char类型的数组,来存储字符串变量

注:C语言中,没有专用的字符串类型。

在C++中,使用std::string类型来表示字符串变量。

C++风格的字符串string:

string变量的定义、初始化

//定义了一个字符串变量,此时,该字符串是空字符串。

      string girlFriend1;

      girlFriend1 = "王菲"//把字符串常量"王菲"拷贝到girlFriend1

      cout << "girlFriend1 = " << girlFriend1 << endl;

      string girlFriend2;

      girlFriend2 = girlFriend1; //把字符串变量girlFriend1的值拷贝到girlFriend2

      cout << "girlFriend2 = " << girlFriend2  << endl;

      //定义girlFriend3的同时,使用字符串常量"周迅"来初始化

      string girlFriend3("周迅");

      cout << "girlFriend3 = " << girlFriend3  << endl;

      //定义girlFriend4的同时,使用字符串变量来初始化

      string girlFriend4(girlFriend3);

      cout << "girlFriend4 = " << girlFriend4  << endl;

      string girlFriend5(10, 'A');

      cout << "girlFriend5 = " << girlFriend5  << endl;

string 变量的输入、输出

使用std::cin >> 输入

从第一个非空白字开始,直到遇到空白字符时停止输入

空白字符是指:空格,制表符,回车符

使用std::cout  <<  输出

demo1-基本的输入输出

string job;

       cout << "你是做什么工作的?" << endl;

       cin >> job;

       cout << "做" <<  job << ",收入一定不错吧?" << endl;

demo2-自动跳过空白字符

       string university;  //大学

       string profession; //专业

       cout << "你是哪个学习毕业的?学什么专业? ";

       // 输入:     清华     考古     hello

       // 自动跳过空白字符

       cin >> university >> profession;

       cout << university << "的" << profession << "专业不错哦!"  << endl;

demo3 -连续输入多个字符串,个数不确定

#include <iostream>

#include <Windows.h>

#include <string>

using namespace std;

int main(void) {

       string food; //专业

       int i = 0;

      

       cout <<  "你喜欢什么美食? ";

       while (cin >> food) { //当用户输入 Ctrl + z 并回车 cin >> food返回0,  0就是假

              i = i + 1;

              cout << "你喜欢的第" << i << "美食是:" << food << endl ;

              cout <<  "你还喜欢吃什么美食? ";

       }

       cout << "你最喜欢的美食有" << i << "种"  << endl;

       system("pause");

       return 0;

}

demo4 - 读取一行getline

#include <iostream>

#include <Windows.h>

#include <string>

using namespace std;

int main(void) {

       string addr; //专业

      

       cout << "你想到哪里去旅行:";

       //从标准输入设备读取一行,但是不包括最后输入的回车符

       getline(cin, addr);

       //empty方法

       if (addr.empty() == true) {

              cout << "您输入了一个空行" << endl;

              return 1;

       }

       //size()和length()完全等效

       //长度是指字符串占用的字节数,如果含有汉字,那么总的字节数和汉字个数不同

       cout << "地址的长度是:" << addr.size() << endl;

       cout << "地址的长度是:" << addr.length() << endl;

       system("pause");

       return 0;

}

string字符串的比较

比较规则:和C语言相同。

从字符串的第一个字符开始,对应字符逐个比较,直到遇到不相等的字符为止。

比较运算符有:

>   >=   <    <=   ==   

比较运算的结果:逻辑真,  逻辑假

“123”  <  “1230”          真

“19”   >   “123456789”     真

“2”  >   “1999”           真

“123”  ==  “123”            真

“123”  ==  “1230”           假  

#include <iostream>

#include <Windows.h>

#include <string>

using namespace std;

int main(void) {

      string myGirl = "小芳";

      string yourGirl;

      cout << "我喜欢的女孩子是" << myGirl << endl;

      cout << "你喜欢的女孩是:";

      cin >> yourGirl;

      if (myGirl == yourGirl) {

            cout << "我们喜欢同一人,过来决斗吧" << endl;

      } else {

            cout << "祝你幸福";

      }

      system("pause");

      return 0;

}

注意:c语言的字符串,不能使用这个方式来进行字符串比较。

string字符串的加法

把+左侧的字符串,和+右侧的字符串,直接拼接成一个新的字符串

注意顺序。(C语言的字符串不支持该方式)

注意与数学中的“加法”不同:

“100” + “200”  等于 “300” 

“100” + “200” 等于 “100200”

#include <iostream>

#include <Windows.h>

#include <string>

using namespace std;

int main(void) {

       string s1 = "武当派";

       string s2 = "张三丰";

       string s3 = "太极拳";

       string s4;

       s4 = s1 + s2;  //武当派张三丰

       cout << "s4=" << s4 << endl;

       s4 = s4 + s3;  //武当派张三丰太极拳

       //相当于 s4 += s3;

       cout << "s4=" << s4 << endl;

       s4 += "第一式";  

       //等效于:s4 = s4 + "第一式";

       cout << "s4=" << s4 << endl;

       system("pause");

       return 0;

}

C语言风格的字符串char数组

在c语言中,字符串是以“字符数组”存储的。

#include <iostream>

#include <Windows.h>

#include <stdio.h>

using namespace std;

int main(void) {

          //C语言风格的字符串

         char name[32];

        

         /*

         使用C语言的函数,来处理C语言字符串

         printf("请输入您的名字:");

         scanf("%s", name);       

         printf("%s, 你好!\n", name);  

         */

         // 使用C++的方式,来处理C语言字符串

         cout << "请输入您的名字:";

         cin >> name;

         cout << name << ",你好!" << endl;

         system("pause");

         return 0;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

会飞的鱼-blog

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值