如何从C ++中的字符串中的空格中提取子字符串? [英] How to extract substring between whitespaces from a string in C++?

查看:135
本文介绍了如何从C ++中的字符串中的空格中提取子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个程序来提取空格之间的子字符串

所以如果用户输入Hello My Friend

我希望我的程序定义新字符串,其中: string1 =你好;

string2 =我的;

string3 =朋友;





我们怎么办呢?



我尝试过:



我尝试使用string1 = string.substr(0,string.find());

但这只会提取第一个单词..

有没有简单明了的方法呢?

解决方案

用一些分隔符拆分字符串是一个经常使用的函数,所以每个更好的String类都有这样的能力。



微软有 Tokenize 它的字符串类,这里是示例来自std:string类。

这里有一些想法:用C ++拆分字符串? - 堆栈溢出 [ ^ ]。

I want to write a program to extract substring between whitespaces
so if the user enters "Hello My Friend"
I want my program to define new strings where : string1="Hello";
string2="My";
string3="Friend";


how can we do that ?

What I have tried:

I tried using string1=string.substr(0,string.find(" "));
but this would only extract the first word ..
is there any short and easy way to do it ?

解决方案

Splitting a string with some delimiter is an often used function, so every better String class has such capability.

Microsoft has the
Tokenize in its string class and here is asample from the std:string class.


Some ideas here: Split a string in C++? - Stack Overflow[^].


这篇关于如何从C ++中的字符串中的空格中提取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆