# searching a string for mulitple characters

• 04-27-2002
cxs00u
searching a string for mulitple characters
hi,

I am writting a program which has to search through strings. Up until now I have been using the code

string.find_first_of("a")

for example. The problem now is that I now need to search through for one of four characters + - / * and return the position of the first symbol it comes across or -1 if it can not find any.

Can I do this using the find_first_of method or do I have to do it another way ??

Thank you for your help

Craig
• 04-27-2002
Alipha
(I'm not familiar with the find_first_of() method, but I assume it returns the character position in the string or -1 if not found)

Well, you can find the first instance of +, -, *, and / then compare them to see which appeared first:

plus = string.find_first_of("+");
minus = string.find_first_of("-");
multiply = string.find_first_of("*");
divide = string.find_first_of("/");

first = -1;

if(plus != -1)
if((first == -1) || (plus < first))
first = plus;
if(minus != -1)
if((first == -1) || (minus < first))
first = minus;
if(multiply != -1)
if((first == -1) || (multiply < first))
first = multiply;
if(divide != -1)
if((first == -1) || (divide < first))
first = divide;