istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string

简介:  1. istringstream字符串流 #include <iostream> #include <sstream> #include <string>   using namespace std;   struct MyStruct {     string str1,


1. istringstream字符串流

#include <iostream>

#include <sstream>

#include <string>

 

using namespace std;

 

struct MyStruct

{

    string str1, str2, str3;

    double db;

    int num;

    char ch;

};

 

void main()

{

    string  mystring("china  google microsoft 12.9 123 A");

    MyStruct struct1;

   

    istringstream input(mystring);//创建一个字符串扫描流

    input >> struct1.str1 >> struct1.str2 >> struct1.str3 >> struct1.db >> struct1.num >> struct1.ch;

    cout << struct1.str1 << endl;

    cout << struct1.str2 << endl;

    cout << struct1.str3 << endl;

    cout << struct1.db << endl;

    cout << struct1.num << endl;

    cout << struct1.ch << endl;

 

    cin.get();

}

2.实现类似字符串截取的功能

#include <iostream>

#include <sstream>

#include <string>

 

using namespace std;

//实现类似字符串截取的功能

void main()

{

    char mystring[50] = "china#123#A";

    for (char *p = mystring; *p != '\0'; p++)

    {

        if (*p == '#')

        {

            *p = ' ';

        }

    }

    istringstream input(mystring);//创建一个字符串扫描流

    string str;

    int num;

    char ch;

    input >> str >> num >> ch;

 

    cout << str << endl;

    cout << num << endl;

    cout << ch << endl;

 

    cin.get();

}

运行结果:

3.实现类似字符串截取的功能

#include <iostream>

#include <sstream>

#include <string>

 

using namespace std;

//实现类似字符串截取的功能

void main()

{

    ostringstream  MYOUT;

    char str[100] = { 0 };

    //ostringstream MYOUT(str,sizeof(str));

 

    char str1[50] = "a1234567b";

 

    MYOUT << "a1234b" << " " << 123<< ""<< 234.89 << " " << 'h' << " " << str1 << endl;

    cout << MYOUT.str();

    //cout <<str;

 

    cin.get();

}

运行结果如下:

4.字符串流中的put

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <sstream>

#include <string>

#include <stdlib.h>

 

using namespace std;

void main()

{

    stringstream mystr;//字符串进行输入

    mystr.put('X').put('Y');//连个字符输入

    mystr << "ZXCV";//字符串输入

    cout << mystr.str();

 

    string str = mystr.str();//定义字符串接受值

 

    char ch;    //从字符串内部读取一个字符

    mystr >> ch;

    cout << "\n";

    cout.put(ch);

 

    cout << "\n";

    cout << mystr.str();

    std::cin.get();

    system("pause");

}

运行结果

5.str()将流转换成为字符串string

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <sstream>

#include <string>

#include <stdlib.h>

 

using namespace std;

void main()

{

    stringstream mystr;//sprintf功能

    char cmd1[30] = { 0 };

    char cmd2[30] = { 0 };

    cin.getline(cmd1, 30).getline(cmd2, 30);//输入两个字符串

    mystr << cmd1 << "&" << cmd2;//字符打印

    string str = mystr.str();//定义字符串接受值

    system(str.c_str());

 

    char cstr[50] = { 0 };//默认的字符串

    strcpy(cstr, str.c_str());

    cout << cstr << endl;

    for (char *p = cstr; *p != '\0'; p++)

    {

        if (*p == '&')

        {

            *p = ' ';

        }

    }

    char newcmd1[30] = { 0 };

    char newcmd2[30] = { 0 };

    stringstream  newstr(cstr);//sscanf的功能

    newstr >> newcmd1 >> newcmd2;

    cout << newcmd1 << "\n" << newcmd2 << endl;

 

    system("pause");

}

 

目录
相关文章
|
1月前
|
存储 缓存 测试技术
CMake String函数:如何巧妙地在cmake中操作字符串
CMake String函数:如何巧妙地在cmake中操作字符串
86 0
|
1月前
|
索引
String类的常用功能
String类的常用功能
18 0
|
存储 编译器 Linux
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
标准库中的string类(中)+仅仅反转字母+字符串中的第一个唯一字符+字符串相加——“C++”“Leetcode每日一题”
|
15天前
|
JavaScript
js 字符串String转对象Object
该代码示例展示了如何将一个以逗号分隔的字符串(`&#39;1.2,2,3,4,5&#39;`)转换为对象数组。通过使用`split(&#39;,&#39;)`分割字符串并`map(parseFloat)`处理每个元素,将字符串转换成浮点数数组,最终得到一个对象数组,其类型为`object`。
|
18天前
|
XML 编解码 数据格式
Python标准数据类型-String(字符串)
Python标准数据类型-String(字符串)
23 2
|
1月前
|
SQL JavaScript
js开发:请解释什么是ES6的模板字符串(template string),并给出一个示例。
ES6的模板字符串以反引号包围,支持变量和表达式插入以及多行书写。例如,插入变量值`Hello, ${name}!`,计算表达式`${num1 + num2}`,以及创建多行字符串。模板字符串保留原始空格和缩进,简化了字符串拼接,提高了代码可读性。
18 6
|
1月前
|
SQL Java
使用java中的String类操作复杂的字符串
使用java中的String类操作复杂的字符串
9 0
|
1月前
|
存储 编译器 C语言
【C++】——string的功能介绍及使用
【C++】——string的功能介绍及使用
【C++】——string的功能介绍及使用
|
1月前
String类及相应的字符串操作方法
String类及相应的字符串操作方法
68 1
|
1月前
|
存储 程序员 C++
在C++语言中string-vector的字符串作用类型
在C++语言中string-vector的字符串作用类型
9 0