Android NDK开发之旅27--C++--string类的用法总结

Android NDK开发之旅 目录

1.string类-初始化

#include <iostream>
//cout输出 str 引用sstream  Android里可以省略此引用  使用#include <android/log.h>
#include <sstream>
using namespace std;
void main() {

    //方式一
    string str1 = "aaaa";
    cout << "str1:"<< str1 << endl;

    //方式二
    string str21 = string("bbbba");
    string str22("bbbbb");
    cout << "str21:" << str21 << endl;
    cout << "str21:" << str22 << endl;

    //方式三
    string str = "ccccc";
    string str3 = str;
    cout << "str3:" << str3 << endl;

    //方式四
    string str40 = "ddddd";
    string str4(str40);
    cout << "str4:" << str4 << endl;

    //方式五:连续拼接6个'e'
    string str5(6, 'e');
    cout << "str5:" << str5 << endl;

    //方式六:指针方式
    string* str6 = new string("fffff");
    cout << "str6:" << *str6 << endl;
    //方式七:设置为NULL
    //在C++中string是一个类,底层实现也是C语言中的char*
    string*  str7 = NULL; //注意string  str7 = NULL 报错; 

    getchar();
}

执行输出:

str1:aaaa
str21:bbbba
str21:bbbbb
str3:ccccc
str4:ddddd
str5:eeeeee
str6:fffff

2.string类-遍历

#include <exception>  
#include <iostream>
//cout输出 str 引用sstream  Android里可以省略此引用  使用#include <android/log.h>
#include <sstream>
using namespace std;


void main() {
    string str = "Android";
    cout << "数组遍历方式" << endl;
    //方式一:数组遍历(在AS中越界不会报错,内部做了处理)
    for (int i = 0; i < str.length() ; ++i) {
        //内部做了处理,如果下标越界,那么返回空'\0'
        char c = str[i];
    
        cout << "第" << i << "值:" << c << endl;
    }
    cout << endl << "迭代器方式"  << endl;

    //方式二:迭代器
    int i = 0;
    for (string::iterator it = str.begin(); it != str.end(); it++) {
        char c = *it;
        cout << "第" << i << "值:" << c << endl;
        i++;
    }
    cout << endl << "at函数遍历方式" << endl;
    //方式三:at函数遍历(越界,抛异常)
    //在Java中charAt(i),C++中at(i)
    //捕获异常:兼容Android NDK做一些配置
    //在Android NDK开发中r5之前默认不支持C++中的try catch
    //但是在Android NDK R5开始支持C++中的try catch
    //但是Android NDK编译器默认异常控制特效关闭状态,所以手动的打开异常控制
    //打开配置:我们需要在gradle配置文件中配置(cppFlags = "-fexceptions")即可
    try {
        for (int i = 0; i < str.length() + 3; ++i) {
            char c = str.at(i);
            cout << "第" << i << "值:" << c << endl ;
        }
    }
    catch (...) {
        cout << "发生了异常......" << endl;

    }
    getchar();

}

执行输出:

数组遍历方式
第0值:A
第1值:n
第2值:d
第3值:r
第4值:o
第5值:i
第6值:d

迭代器方式
第0值:A
第1值:n
第2值:d
第3值:r
第4值:o
第5值:i
第6值:d

at函数遍历方式
第0值:A
第1值:n
第2值:d
第3值:r
第4值:o
第5值:i
第6值:d
发生了异常......

3.string类和char之间的转换

#include <exception>  
#include <iostream>
//cout输出 str 引用sstream  Android里可以省略此引用  使用#include <android/log.h>
#include <sstream>
using namespace std;

void main() {
    //3.1 string转成char*
    string str = "Android";
    const char* crr1 = str.c_str();

    cout << crr1 << endl;
    //3.2 string转成char[]数组
    char crr2[20] = { 0 };
    //第一个参数:拷贝目标
    //第二个参数:拷贝的数量
    //第三个参数:从哪里开始
    str.copy(crr2, str.length(), 0);
    cout << crr2 << endl;
}

执行输出:

Android
Android

4.string类-字符串查找

#include <iostream>
//cout输出 str 引用sstream  Android里可以省略此引用  使用#include <android/log.h>
#include <sstream>
using namespace std;
void main() {


    string str = "Android Developer love Android";

    cout << "查找从指定位置开始的字符串起始位置" << endl;
    //4.1 查找指定的字符串:find(顺时针)
     //参数一:查找条件
     //参数二:从那个位置开始查找,默认是0开始
    int index = str.find("Android", 0);
    cout << "值:" << index << endl;

    //未找到返回-1
    int index1 = str.find("IOS", 0);
    cout << "值:" << index1 << endl;

    cout << endl << "查找从末尾指定位置开始的字符串末尾位置" << endl;
    //4.2 查找最后一个字符串:find_last_of(逆时针)

    int index2 = str.find_last_of("Android", str.length());


    cout << "值:" << index2 << endl;

    cout << endl << "查找某个字符串出现的次数" << endl;
    //4.3 查找某个字符串出现的次数
    int index3 = str.find("Android", 0);
    //计数
    int count = 0;
    //string::npos默认值是:-1
    //判断是否存在
    while (index3 != string::npos) {
        count++;
        index3 = index3 + 5;
        index3 = str.find("Android", index3);
    }
    cout << "次数:" << count << endl;
    getchar();
}

执行输出:

查找从指定位置开始的字符串起始位置
值:0
值:-1

查找从末尾指定位置开始的字符串末尾位置
值:29

查找某个字符串出现的次数
次数:2

5.string类-字符串替换

#include <iostream>
#include <sstream>
using namespace std;
void main() {
    //5.1 替换一个
        string str0 = "Android Developer love Android";
        str0.replace(0,3,"Hello");
        cout << str0.c_str() << endl;

        string str1 = "Android Developer love Android";
        str1.replace(0, 7, "Hello");
        cout  << str1 << endl;

    //5.2 替换所有
    string str = "Android Developer love Android";
    int index = str.find("Android", 0);
    while (index != string::npos) {
        str.replace(index, 7, "Hello");
        index = index + 7;
        //继续往后查询
        index = str.find("Android", index);
    }
    cout  << str.c_str() << endl;

    getchar();
}

执行输出:

Helloroid Developer love Android
Hello Developer love Android
Hello Developer love Hello

6.string类-字符串删除

#include <iostream>
#include <sstream>
using namespace std;
void main() {

    //6.1 删除单个erase函数
    string str = "Android Developer love Android";
    //可以erase(0)代表删除整个字符串,因为删除数量number默认是字符串的长度
    //erase()也是干掉
    //参数有默认值
    //str.erase();
    //正确写法
    string::iterator index0 = find(str.begin(), str.end(), 'A');
    str.erase(index0);
    cout << str.c_str() << endl;

    //6.2 删除多个
    string str1 = "Android Developer love Android";
    //只一个指针
    string::iterator index = find(str1.begin(), str1.end(), 'A');
    str1.erase(index);
    while (index != str1.end()) {
        if (*index == 'A') {
            str1.erase(index);
        }
        //指针位移
        index++;
    }

    cout << str1.c_str() << endl;
    getchar();
}

执行输出:

ndroid Developer love Android
ndroid Developer love ndroid

7.string类-字符串插入

#include <iostream>
using namespace std;
void main() {
    //从头开始插入
    string str1 = "love Android";
    str1.insert(0, "I ");
    cout << str1.c_str() << endl;

    //从最后开始插入
    string str = "Android Developer love";
    str.insert(str.length(), " Android");
    cout << str.c_str() << endl;
    getchar();
}

执行输出:

I love Android
Android Developer love Android

8.string类-大小写转换

#include <iostream>;
#include <algorithm>  
using namespace std;
void main() {
    //8.1 转小写
    string str = "Android Developer love Android";
    //参数一:从那一个位置开始
    //参数二:到那一个位置结束
    //参数三:目标对象
    //参数四:格式(大写、小写)
    //注意:参数四加'::'
    //因为:在全局命名空间中这个tolower不是宏定义,
    //     而是一个具体有实现的函数,所以需要明确命名空间
    //在不同编译环境下有差别
    //例如:在VS 中不需要"::",但是在Android NDK环境下需要加"::"
    transform(str.begin(), str.end(), str.begin(), ::tolower);


    cout << str.c_str() << endl;
    //8.2 转大写
    string str1 = "Android Developer love Android";
    transform(str1.begin(), str1.end(), str1.begin(), ::toupper);

    cout << str1.c_str() << endl;
    getchar();
}

执行输出:

android developer love android
ANDROID DEVELOPER LOVE ANDROID

特别感谢:

Dream

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容