C++如何将string按空格分割?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++如何将string按空格分割?相关的知识,希望对你有一定的参考价值。
参考技术A #include <stdio.h> #include <iostream> #include <vector> void main() std::string o_str = "sadf sdfd asd asdf"; std::vector<std::string> str_list; // 存放分割后的字符串 int comma_n = 0; do std::string tmp_s = ""; comma_n =o_str.find( " " ); if( -1 == comma_n ) tmp_s = o_str.substr( 0, o_str.length() ); str_list.push_back( tmp_s );break; tmp_s = o_str.substr( 0, comma_n ); o_str.erase( 0, comma_n+1 ); str_list.push_back( tmp_s );
while(true); system( "pause" ); #include <stdio.h> #include <iostream> #include <vector> void main() std::string o_str = "sadf sdfd asd asdf"; std::vector<std::string> str_list; // 存放分割后的字符串 int comma_n = 0; do
std::string tmp_s = ""; comma_n = o_str.find( " " ); if( -1 == comma_n ) tmp_s = o_str.substr( 0, o_str.length() ); str_list.push_back( tmp_s ); break; tmp_s = o_str.substr( 0, comma_n ); o_str.erase( 0, comma_n+1 ); str_list.push_back( tmp_s ); while(true); system( "pause" );
C#如何分割多个空格分隔的字符串?
using System;
using System.Text;
using System.Text.RegularExpressions;
namespace test
{
class Program
{
public static void Main(string[] args)
{
string pp="1063792.4 2764405.825 5.464413E-05 -1.780467E-04";
string[] mm=Regex.Split(pp,"\\s+",RegexOptions.IgnoreCase);
for (int i=0;i<mm.Length;i++)
{
Console.Write(mm[i]+‘\n‘);
}
// TODO: Implement Functionality Here
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
以上是关于C++如何将string按空格分割?的主要内容,如果未能解决你的问题,请参考以下文章