Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

jumpjumpjump

What function does this in C++?

This topic is 5245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Since i am moving from blitz to C++ what function is equivilent to mid$ in it? Mid$ does this(from manual): Use this command to grab a set of characters from within a string. You can choose WHERE to start in the string, and how many characters to pick. You''ll probably use this to ''decode'' or ''walk through'' a string to get each character out of it for conversion or validation. See the Example. What do i need to do to do that in C++? Sharp Basic baby!
My Homepage | My Forums Please Register | Google | Sharp Basic - Coming summer 2004! Wanna Beta test? Clicky!

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
std::string::substr()

Share this post


Link to post
Share on other sites
Actually, the substr() method of std::string isn''t static, so.. like this:

#include <string>
#include <iostream>

using std::string;
using std::cout;

int main(int argc, char* argv[])
{
string s("This is my string.");
cout << "Part of the string: " << s.substr(5,5) << "\n";
return 1;
}

Program output:

Part of the string: is my

s.substr(0,4) would return "this". The first argument is the string position to start extraction, the second argument is how many characters to extract.

brett

Share this post


Link to post
Share on other sites
quote:
Original post by aftli
s.substr(0,4) would return "this".

Or rather, to not confuse things, "This".

Share this post


Link to post
Share on other sites
You could always make a function to do it yourself:

In this function, str2 is the one that gets modified
newstrlen is the length you want the new string to be and
lenstr2 is the lenght of the string that is going to be modified

and by modified, i mean, the result will be put in lenstr2.

void mid(char * str1, char * str2, int newstrlen, int lenstr2, int start)
{
int i = 0;

//Copy characters into new string
while (i {
str2 = str1[i + (start-1)];
i++;
}
str2[newstrlen] = ''\0'';

}

Share this post


Link to post
Share on other sites
hmm, for some reason that didn''t post properly.

the condition in the while statement is supposed to be:

while (i

Share this post


Link to post
Share on other sites
quote:
Original post by jumpjumpjump
You''ll probably use this to ''decode'' or ''walk through'' a string to get each character out of it for conversion or validation.
In that case (in the case where you only want one character at a time) use operator[], will be quite a bit faster than using substr.

Share this post


Link to post
Share on other sites
quote:
Original post by squicklid
hmm, for some reason that didn't post properly.

the condition in the while statement is supposed to be:

while (i


HTML is eating your <. Use &lt; (and &amp; )

while (i<newstrlen && i<lenstr2)

[edited by - Doc on April 4, 2004 9:28:40 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!