PDA

View Full Version : Strings...


Wiz
06-05-2002, 04:22 AM
What's the EQemu function for selecting certain letters from a string? Doesn't have a substr, as far as I can see, so I was wondering what's being used instead.

theCoder
06-08-2002, 06:04 AM
You mean from inside of an EQ client? Or from c code? If you mean c code, there's always strstr (man strstr for info) or strchr (man strchr for info). I don't know if someone wrote a specialized string searching function inside the EMU, but it would probably be hard to beat one of the built in functions as far as performance.

Wiz
06-08-2002, 10:53 PM
Yeah, from the C code. Specifically, selecting the "00" etc from an NPC name. I'm trying to cut that off in the GetName() function.

"man"? Sorry, you're dealing with someone with four weeks of C++ experience, heh.

theCoder
06-09-2002, 05:33 AM
man is a command you can use to find information about various things, from other commands to system calls and more. man is short for manual, but it's only available (AFAIK) on UNIX style systems (Linux, MacOSX, etc).

As far as finding the "00" in a null terminated string, you'll probably want to use strstr. strstr takes two parameters: the string to search and the string to search for. It returns a pointer to the position in the first string where the second string is located (or NULL if it's not found). For example:

char[256] name = "Some name00";
char* found = strstr(name, "na");
printf("%s\n", found);

Would print out "name00". In this case, found is actually equal to name+5 in terms of pointer math. It can get confusing if you don't understand how pointers work. I'd suggest you find some reference that explains pointers in c/c++.

To better answer your original question, however, if you wanted to delete the "00" in the name above, you would strstr for "00" and if it was found (return value is not NULL), then deference the pointer and set it to '\0' (the null character):

char* found = strstr(name, "00");
if (found != NULL) *found = '\0';
printf("%s\n", name);

Now it prints out "Some name".

Also, if you do change the data in the string, you should be sure to copy it first or it will change the data in the class (which you probably don't want :))

HTH

Wiz
06-10-2002, 01:26 AM
Exactly what I was looking for, thanks a ton!