Skip to main content
gamedev.net
🔒 Locked

Simple string problem.

Started by The C modest god 12 replies 650+ views
Original Post
The C modest god
The C modest god
I have a string variable, which contains some sort of text. For instance "Filename.src" I want to insert another string, lets say "Right", before the period in the first string. So the result should be a string containing: "FilenameRight.src" What is the easiest way to do this?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
ender7771
ender7771
Are you using , or inferring from your name, a simply an array of characters?
Enigma
Enigma
Assuming C++:
string.insert(string.find('.'), "Right");
Enigma
Illco
Illco
Assuming C:

- find the location of the dot (strchr)
- copy everything before the dot to a new buffer (strcpy)
- cat the insertion string to the new buffer (strcat)
- cat everything from the dot on to the new buffer (strcat)

Illco
The C modest god
The C modest god
Yes I was referring to in C++.
I was looking for information about the string class in msdn, and I couldnt find anything usefull or the methods you mentioned.
There is just many useless information in msdn and you just cant find what you need unless you already know exactly what you are looking for.
How can I find all the members and methods of the string class in msdn?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Illco
Illco
Simple: don't limit yourself to MSDN. The STL has little to do with Microsoft and there is plenty of information available about it outside MSN. To name a few:

C++ Reference
C++ FAQ Lite

Illco
stylin
stylin
Here's something to bookmark then:

MSDN: Standard Library Headers
MSDN: std::basic_string Member Functions

In general, typing in std::any_container in MSDN's search will yield you the page(s) that reference it.

EDIT: and yes, looking elsewhere besides MSDN is always a good idea.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
The C modest god
The C modest god
Quote:
Original post by stylin
Here's something to bookmark then:

MSDN: Standard Library Headers
MSDN: std::basic_string Member Functions

In general, typing in std::any_container in MSDN's search will yield you the page(s) that reference it.

EDIT: and yes, looking elsewhere besides MSDN is always a good idea.


Thanks.
I reached myself the pages in the surroundings of basic_string members page, but didnt notice there is a link to the members of basic strings in one of the other pages.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
The C modest god
The C modest god
Now I have a problem with those strings.
Thats why I dont like to work with those std libraries, you dont know what is going inside there and its not protected from simple errors.
Here is my code:
void SpecificFilesCreator::PushDirectionsAnimation (string ResourceName, string FileName, FileListPointer FLPointer){	string ResourceWork, FileWork, InsertString;	InsertString = "DownLeft";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "LeftLeft";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString); // <= 	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "UpLeft";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "RightLeft";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "DownRight";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "LeftRight";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "UpRight";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();	InsertString = "RightRight";	ResourceWork = ResourceName;	ResourceWork.insert(ResourceWork.length(), InsertString);	FileWork = FileName;	FileWork.insert(FileWork.find('.'), InsertString);	FLPointer.Push(ResourceWork, FileWork);	FLPointer.Step();}


The first time I run this method nothing happens, but when I run it in the following line again, I get an error (user breakpoint) in a place with only std methods calls.
If I switch the first call to this method with the second call, I still get the same error in the second call of this method, so it is not dependant in the parameters passed to the method.

Anyone have any idea what I am doing wrong?
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
daerid
daerid
Wow... That code is... interesting. Seems like you could benefit from some sort of Data Abstraction.
daerid@gmail.com
stylin
stylin
If you just want to append a string onto another string, the most intuitive approach would be (which compiles and runs as exected with VC++ Toolkit 2003):
#include <iostream>#include <string>int main() {   std::string resource_name( "Hello" );   std::string insert_string, resource_temp;      insert_string = ", world!";   resource_temp = resource_name + insert_string;   std::cout << resource_temp << std::endl;   insert_string = ", planet!";   resource_temp = resource_name + insert_string;   std::cout << resource_temp << std::endl;   return 0;}

Usually the simplest solution is the best. As for your error, insert() worked just as well for me, so I have no clue.
:stylin: "Make games, not war." "...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
The C modest god
The C modest god
Quote:
Original post by daerid
Wow... That code is... interesting. Seems like you could benefit from some sort of Data Abstraction.

Do you mean that I didnt insert the repetetive commands into a method?
I dont know why I am actually sometime dont do it.
I guess It is tempting to just mindelessly copy the same code instead of creating a new method.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:
Zahlman
Zahlman
Keeping the name separate from the extension would help a lot here, as would using some looping.

void SpecificFilesCreator::PushDirectionsAnimation (string ResourceName, string FileName, string FileExt, FileListPointer FLPointer) {  string[] firstDirections = { "Down", "Left", "Up", "Right" };  string[] secondDirections = { "Left", "Right" };  for (int i = 0; i < sizeof(firstDirections) / sizeof(string); ++i) {    for (int j = 0; j < sizeof(secondDirections) / sizeof(string); ++j) {      FLPointer.Push(ResourceName + firstDirections + secondDirections[j],                     FileName + firstDirections + secondDirections[j] + FileExt);      FLPointer.Step();    }  }}


You could alternatively use the original method signature by first breaking up the filename into its components and then doing the rest as shown.
The C modest god
The C modest god
Ok I have found the problem.
I passed the FLPointer paramter without reference, that caused the problem.
I guess the consipiracy of the standard string is wrong after all, and it is safe to use string. sigh...
Thanks for all the replies.
It's all about the wheel.Never blindly trust technoligy.I love my internal organs.Real men don't shower. Quote: Original post by Toolmaker Quote:

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.