istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string
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");
}
istringstream字符串流,实现类似字符串截取的功能,字符串流中的put,str()将流转换成为字符串string相关推荐
- [JQuery]jQuery中serializeArray方法的使用及对象与字符串的转换
使用jQuery中的serializeArray()方法可以方便的将表单中的各个信息,转化为多个{name:xx,value:xx}对象的数组, 再使用遍历的方式可以方便的将数组转化为json对象, ...
- js 字符串截取_【js】让你一次性搞清楚slice,substr,substring字符串截取函数
假设 str是个字符串变量,且设置值为"LittleAnn",字符长度是9 那么这三个函数使用方式: var 相同点: start为开始位置下标,end为结束位置下标 下标从0开始 ...
- Java18-day09【字节缓冲流、字符流、编码表、字符串与字符流中的编码解码问题、字符流读写数据的方式、字符缓冲流、IO流小结】
视频+资料(工程源码.笔记)[链接:https://pan.baidu.com/s/1MdFNUADVSFf-lVw3SJRvtg 提取码:zjxs] Java基础--学习笔记(零起点打开java ...
- java字符串切割_java字符串常用操作方法(查找、截取、分割)
如下所示: public class 字符串常用操作 { public static void main(String[] args) { /* * 查找子串 */ String str1=" ...
- python截取字符串函数substr_PHP substr():截取字符串
在对字符串进行处理时,有时我们需要对字符串进行截取操作.在 PHP 中,截取字符串可以通过 PHP 的预定义函数 substr() 来实现,本节我们就来介绍一下 substr() 函数的使用. sub ...
- php截取字符串后编码不对,php截取字符串出现中文乱码问题的解决
在之前的文章中我们给大家接好了php截取字符串的实现,以及php截取中文字符串的使用,那么我们在截取中文字符串的时候,很多时候都会出现乱码的问题,那么我们今天就给大家介绍php截取字符串出现中文乱码问 ...
- java 比较字符串前几位_java截取字符串前几位
java截取字符串_IT/计算机_专业资料.java截取字符串 1根据字符串 S... java中常用的字符串的截取方法_计算机软件及应用_IT/计算机_专业资料.jsp中常见使用技术,js,jstl ...
- JAVA 操作字符串 分割、替换、截取操作
1.按指定字符分割 1.1 String belongPlace = "北京市,天津市,石家庄市";String[] placeName = belongPlace.split(& ...
- java 半个汉字,Java截取字符串军令状汉字不被截取半个
Java截取字符串保证汉字不被截取半个 Java截取字符串保证汉字不被截取半个 public class SplitString { public static void main(String[] ...
最新文章
- Java连接Oracle数据库示例
- 下qemu模拟arm9_QEMU搭建树莓派环境
- 组合部分标签向量并累加成完整向量
- 派生类构造的时候一定要调用_为什么骑车的时候一定要带手套?
- PADS中Layer25层的作用(Z)
- 为什么我不能关闭垃圾收集器?
- pythonqt5plaintextedit某一行的内容_如何能够做到持续输出内容?
- IE6下溢出多余文字
- VS2005里自定义控件设计时语法
- php数组array_filter,php数组array_filter()函数和array_slice()函数
- 周杰 清华大学计算机学院,周杰 -清华大学自动化系
- 微机原理与接口技术-第二版-课后习题答案 绪论
- GPU cuda驱动安装
- matlab解三次方程,并且输出图像
- swiper+vue3,使用自动切换autoplay+自定义分页器navigation的报错问题解决
- html页面栅格系统,超好用的网页栅格化工具: GridGuide
- 甲骨文公司总裁Larry Ellison在耶鲁大学的演讲
- 安全教育平台登录显示服务器繁忙,安全教育平台登录失败是怎么回事 解决方法...
- OperationException: CLIENT: CLIENT_ERROR cannot increment or decrement non-numeric value
- 2012, PPSN,Geometric Semantic Genetic Programming,GSGP
热门文章
- Python库引用import多种用法及比较
- Python学习笔记--程序控制结构
- wxWidgets:wxStdInputStreamBuffer类用法
- wxWidgets:线程间和进程间通信
- boost::mpl模块实现list_c相关的测试程序
- boost::mp11::mp_max相关用法的测试程序
- boost::graph模块实现DFS parenthesis的测试程序
- DCMTK:使用RLE传输语法压缩DICOM文件
- OpenCV运行ReID网络的实例(附完整代码)
- OpenCV支持向量机SVM和SDG算法的实例(附完整代码)