28

For example, if I have

string x = "dog:cat";

and I want to extract everything after the ":", and return cat. What would be the way to go about doing this?

SKLAK
  • 3,325
  • 9
  • 27
  • 51

9 Answers9

73

Try this:

x.substr(x.find(":") + 1); 
rcs
  • 5,312
  • 9
  • 45
  • 61
  • This is amazing !! <3 – Spandyie Jun 05 '19 at 01:21
  • 1
    This creates a copy, which may not be what you want. In C++17 you can use [`std::string_view`](https://stackoverflow.com/questions/20803826/what-is-string-view) to avoid copying. – Kyle Aug 21 '19 at 18:23
  • 2
    As mentioned in some of the other answers, you should handle the edge case of `find` returning `npos`. It is not guaranteed that `npos + 1` equals 0 (see https://stackoverflow.com/questions/26402961/is-it-guaranteed-by-c-standard-that-stdbasic-stringnpos-1-0). – galsh83 Sep 19 '19 at 06:43
  • What if I had "cat:dog:parrot:horse" and I want to get just horse? (So the last one :) – User123 Mar 11 '20 at 14:57
10

I know it will be super late but I am not able to comment accepted answer. If you are using only a single character in find function use '' instead of "". As Clang-Tidy says The character literal overload is more efficient.

So x.substr(x.find(':') + 1)

Newbie
  • 392
  • 5
  • 15
7

The accepted answer from rcs can be improved. Don't have rep so I can't comment on the answer.

std::string x = "dog:cat";
std::string substr;
auto npos = x.find(":");

if (npos != std::string::npos)
    substr = x.substr(npos + 1);

if (!substr.empty())
    ; // Found substring;

Not performing proper error checking trips up lots of programmers. The string has the sentinel the OP is interested but throws std::out_of_range if pos > size().

basic_string substr( size_type pos = 0, size_type count = npos ) const;
Edward Kigwana
  • 134
  • 1
  • 5
4
#include <iostream>
#include <string>

int main(){
  std::string x = "dog:cat";

  //prints cat
  std::cout << x.substr(x.find(":") + 1) << '\n';
}

Here is an implementation wrapped in a function that will work on a delimiter of any length:

#include <iostream>
#include <string>

std::string get_right_of_delim(std::string const& str, std::string const& delim){
  return str.substr(str.find(delim) + delim.size());
}

int main(){

  //prints cat
  std::cout << get_right_of_delim("dog::cat","::") << '\n';

}
Trevor Hickey
  • 31,728
  • 22
  • 131
  • 236
1

Try this:

  string x="dog:cat";
  int pos = x.find(":");
  string sub = x.substr (pos+1);
  cout << sub;
Harikrishnan N
  • 704
  • 9
  • 18
1

What you can do is get the position of ':' from your string, then retrieve everything after that position using substring.

size_t pos = x.find(":"); // position of ":" in str

string str3 = str.substr (pos);

1
#include <string>
#include <iostream>
std::string process(std::string const& s)
{
    std::string::size_type pos = s.find(':');
    if (pos!= std::string::npos)
    {
        return s.substr(pos+1,s.length());
    }
    else
    {
        return s;
    }
}
int main()
{
    std::string s = process("dog:cat");
    std::cout << s;
}
eyllanesc
  • 190,383
  • 15
  • 87
  • 142
0

Try this one..

std::stringstream x("dog:cat");
std::string segment;
std::vector<std::string> seglist;

while(std::getline(x, segment, ':'))
{
   seglist.push_back(segment);
}
Dyrandz Famador
  • 4,303
  • 5
  • 18
  • 38
0

something like this:

string x = "dog:cat";
int i = x.find_first_of(":");
string cat = x.substr(i+1);
Cristian Olaru
  • 458
  • 4
  • 19