954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Getting a substring substring, but without 2 strings

Hello, I just have a question about how to find a substring when one has only a string and coordinates for where the data will be? For instance, a string "why,hello", and data that is at the coordinates 4 to 8.

strstr, I obviously cannot use directly, because I have no idea what the string I am searching for is named.

I could use a loop that begins at 4 (just as an example; it would be a variable) and goes until 8, recording each character in a new character array. However, I have previously done this often only to find out that strcat(), memcpy(), or some other predefined function was already made and could probably work better than my code. :o

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

You can use

substr()


function of string class

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

Of course. I hadn't seen that in the cplusplus.com reference for cstring. In fact, cplusplus.com has apparently no documentation for substr at all.... Ah well, thanks for the function.

ilikerps
Light Poster
45 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 
Of course. I hadn't seen that in the cplusplus.com reference for cstring. In fact, cplusplus.com has apparently no documentation for substr at all.

substr() is not a function of cstring -- it is a method of c++ std::string class.

#include <string>

int main()
{
std::string newstr;
std::string str = "why,hello";

int pos = str.find(',');
if(pos > 0)
  mewstr = str.substr(pos+1);

return 0;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You