• ### Announcements

#### Archived

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

# remove part or char *...

## Recommended Posts

Hi, I have this char* = , for instance, "texture.tga". How do I remove, using C/C++, the ".tga", from it, and return only "texture" ? Thanks

##### Share on other sites
Find the last index of ''.'' and replace it with ''\0''.

##### Share on other sites
a char* is an array so you can loop it like an array. Something like :

for (int j = (strlen(myStr) - 1); j >= 0; --j)
if (myStr[j] == '.')
{
myStr[j] = '\0';
break;
}

of course i wrote that off the top of my head so there is bound to be some random error in there but you get the idea. If you replace the '.' with the null char '\0' then the string will end there so .tga will dissapear

pto

(NOTE : found one random error already )

[edited by - kaysik on July 6, 2003 5:49:48 AM]

##### Share on other sites
It depends how it is declared. If it is like this:
char* texture="texture.tga";
Then no you can''t do that since the above is the exact same as this:
const char* texture="texture.tga";
However, if you declare it like this:
char texture[]="texture.tga";
Then kaysik''s method will work.

-If you see this image I may or may not be online
My website

##### Share on other sites
First of all I''d reccomend switching to the STL string class, much easier way of handling strings. Then you have heaps of algorithms coded into the class. Methods that can find the last instance of a character, find substrings, etc...

-----------------------------
Using C (I'm not familiar with C++) the simplest way would be

*strrchr(myString, '.') = '\0';

Assuming you did one of the following

char myString[] = "texture.tga";

or

char *myString = malloc(strlen("texture.tga") + 1);strcpy(myString, "texture.tga");

As mentioned before,
char *myString = "texture.tga";
is much like
const char *myString = "texture.tga";
except that it'll only fail at runtime on some implementations (but not necessarily other implementations).

EDIT: Fix tags and realize that my C++ idea doesn't work

[edited by - Way Walker on July 6, 2003 3:21:33 PM]

##### Share on other sites
C++ standard library would do something like this

int ext;string myString = "texture.tga";if ( (ext = myString.find(''.'')) != string::npos)   myString = myString.substr(0, ext);

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627662
• Total Posts
2978519

• 10
• 10
• 12
• 22
• 13