1

Possible Duplicate:
Is There A Built-In Way to Split Strings In C++?

i have one string variable which contains: string var = "this_is.that_alos" how i can separate only "that_alos" from it. i.e. i need only second part after "that_alos"

Community
  • 1
  • 1
balaji
  • 35
  • 2
  • 4
  • 1
    You need to be more precise with what you want... Will the string have only one '.' in it? If not, do you want the part after the first '.', the last '.', or is there some other method you would want to use? – jswolf19 Mar 08 '11 at 09:03
  • 1
    And maybe consider deleting the `C` tag, if you're _really_ using C++. My answer below assumes C is fine, because that's what I'm familiar with :) but if you really want a C++-idiomatic answer, please update the tags and I'll delete my answer. Thanks – sarnold Mar 08 '11 at 09:05
  • plz don't use "pls" and other such abbreviations. – John Saunders Mar 10 '11 at 03:41

6 Answers6

3
std::string var = "this_is.that_alos";
std::string part = var.substr(var.find_first_of('.'));

If you want to exclude the '.', just add 1:

std::string part = var.substr(var.find_first_of('.') + 1);
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • it is working but im getting "." also in the part string. how to avoid "." – balaji Mar 08 '11 at 09:17
  • 4
    @balaji: See my edit. I strongly recommend you read the documentation for these methods. This is a very simple task, and with the slightest amount of search, you could have solved it yourself. – Björn Pollex Mar 08 '11 at 09:23
  • 4
    I think that by answering questions like this we're actually doing the OP a disservice and ourselves as well by unleashing very bad programmers with no research or thinking skills on the world. – Jim Balter Mar 08 '11 at 14:07
2

In C, the usual function is strchr(3):

char * second_half = strchr(var, '.');

Of course, this is just a new pointer to the middle of the string. If you start writing into this string, it changes the one and only copy of it, which is also pointed to by char *var. (Again, assuming C.)

sarnold
  • 102,305
  • 22
  • 181
  • 238
0
std::string var = "this_is.that_alos";

std::string::iterator start = std::find(var.begin(), var.end(), '.')
std::string second(start + 1, var.end());

But you should check if start + 1 is not var.end(), if var don't contain any dot.

Fox32
  • 13,126
  • 9
  • 50
  • 71
0

If you don't want to use string indexing or iterators, you can use std::getline

std::string var = "this_is.that_alos";
std::istringstream iss(var);

std::string first, second;

std::getline(iss, first, '.');
std::getline(iss, second, '.');
hrnt
  • 9,882
  • 2
  • 31
  • 38
0

Use strtok for splitting the String, with the Special character as "." for ex

string var = "this_is.that_alos";
string Parts[5] = strtok(&var[0],".");
string SubPart = Parts[1];

Hope it will help U.

Shashwat
  • 2,538
  • 7
  • 37
  • 56
0

The easiest solution would be to use boost::regex (soon to be std::regex). It's not clear what the exact requirements are, but they should be easy to specify using a regular expression.

Otherwise (and using regular expressions could be considered overkill for this), I'd use the usual algorithms, which work for any sequence. In this case, std::find will find the first '.', use it with reverse iterators to find the last, etc.

-- James Kanze

James Kanze
  • 150,581
  • 18
  • 184
  • 329