开发者

could anyone suggest me a simple way of splitting names in C++

开发者 https://www.devze.com 2023-02-03 10:25 出处:网络
I\'ve been t开发者_StackOverflow中文版rying to split a name into first and last name, but I\'m sure my implementation is not the best one as far as simplicity.

I've been t开发者_StackOverflow中文版rying to split a name into first and last name, but I'm sure my implementation is not the best one as far as simplicity.

string name = "John Smith";
    string first;
    string last (name, name.find(" "));//getting lastname
    for(int i=0; i<name.find(" "); i++)
    {
        first += name[i];//getting firstname
    }
    cout << "First: "<< first << " Last: " << last << endl;

Thanks in advance


How about using the substr method from string to split things up combined with find:

std::string name = "John Smith"
std::size_t pos = name.find(" ");
std::cout << "First: " << name.substr(0, pos) << " Last: " << name.substr(pos, std::string::npos) << std::endl;

where I've also used the std::string::npos to indicate the last position of a string. Techincally, I could just get away with name.substr(pos) as npos is the default parameter.

Also, see this SO post about string splitting. You'll find better items there, like mention of the Boost split function.


If there are undetermined number of leading, trailing, and in-between spaces (and/or tabs), then the following is a very clean (but not necessarily most efficient) alternative that I can suggest:

std::istringstream ssname( name ); // needs <sstream> header
string first, last;
ssname >> first >> last;


Can try

strtok

function to split the string, code will be clean


Extending the idea by sly:

#include <map>
#include <memory>
#include <functional>
#include <algorithm>
#include <iostream>
#include <sstream>


int main()
{
    std::string             name    = "   Martin Paul Jones   ";
    std::string::size_type  s       = name.find_first_not_of(" \t\n\r");
    std::string::size_type  e       = name.find_last_not_of(" \t\n\r");
    std::string             trim    = name.substr(s, (e - s + 1));
    std::string             first   = trim.substr(0, trim.find_first_of(" \t\n\r"));
    std::string             last    = trim.substr(trim.find_last_of(" \t\n\r") + 1);

    std::cout << "N(" << name << ") " << " T(" << trim << ") First(" << first << ") Last(" << last << ")\n";

    // Alternative using streams
    std::stringstream       namestream(name);
    namestream >> first >> last;
    while(namestream >> last) { /* Empty */ } // Skip middle names
    std::cout << "N(" << name << ")  First(" << first << ") Last(" << last << ")\n";
}

Try:

> g++ xx.cpp
> ./a.out
N(   Martin Paul Jones   )  T(Martin Paul Jones) First(Martin) Last(Jones)
N(   Martin Paul Jones   )  First(Martin) Last(Jones)


@ Akhil :: I would not recommend to use strtok in a c++ program as it cannot be used to run multiple instances of strtok as it uses a static variable in its implementation. Well you can ofcourse use it if you are going to use a single instance at any point of time..but better to go with c++ design patterns wheb you are using c++ :)

0

精彩评论

暂无评论...
验证码 换一张
取 消