Help converting parse from Processing - indexOf vs ofFind?

Hello again, I am trying to convert the following code from one of my sketches in processing.

void parseWheel(String incomingOSC) {
		for (int i = 0; i < listenTargets.length; i++) { //iterate and listen for listenTargets
			String[] checkString = match(incomingOSC, listenTargets[i]);
			if (checkString != null) {
				//Since checkString was not empty, a target was found.
				//From here, I take that string and strip it down to route it.

Basically, I have an array of strings I am listening for (listenTargets). My source spits hundreds of lines a second. The goal of this function is to find a target then route the information in that string.

I am having trouble with vectors and ofFind, I am not sure it works like I am expecting… Any help would be much appreciated.

assuming incomingOSC and listenTargets[i] are c++ strings, you could do like

for (std::string & s : listenTargets) {
    if (incomingOSC.find(s) != std::string::npos) {
        //found it, do something
    }
}

this will find all the items in listenTargets that is a (sub)string of incomingOSC

you can use the index based for loop too, its all fine.

You can find info on the string find thing here

http://www.cplusplus.com/reference/string/string/find/

Thank you for the reply! That works, except it only returns the matched text.
I was playing around with find before, but this code

for (int i = 0; i < sizeof(listenTargets); i++) {
        if (incomingOSC.find(listenTargets[i]) != std::string::npos) {
            cout << i << endl;
        }
    }

gives me this error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x7ffee0d18000)

EDIT:
Example incomingOSC strings =

"Intens [20]"
"Pan [0]"
"Tilt [0]"

So, listenTargets may look like

listenTargets[0] = "Intens";
listenTargets[1] = "Tilt";

Theres a ton more lines, but just using this as an example. I also can’t just use the order because the order may change.

EDIT2:
Continuing the if statement from my original post. I would then take the string’s substr to get the value between “[” and “]”. Since I am using an iterator in the for loop, I then just used a switch statement to route it. Of course I am open to different solutions, but I at least just need to get that value, and the ability to send it to the correct place.

sizeof gives you the size in bytes of the underlying data type, not the number of elements in an array (if it is an array). You should use i < listenTargets.size()

For getting the substrings, have a look at http://www.cplusplus.com/reference/string/string/substr/

Parsing strings is a bit of a pain in c++ (and java), have you looked at any of the oF OSC addons? I believe it ships with oF by default, and looks like theres a bunch on https://ofxaddons.com/categories/6-web-networking

Interesting… when I try

for (int i = 0; i < listenTargets.size(); i++) {

I get the error: Member reference base type ‘std::__1::string [14]’ is not a structure or union

EDIT:
after more rummaging around stack overflow, I was able to find this.

for (int i = 0; i < (sizeof(listenTargets)/sizeof(*listenTargets)); i++) {

Still don’t totally understand pointers, but this works for now!

I would switch listenTargets to a vector .

ie:

in your .h:

vector <string> listenTargets;

in testApp::setup(){

listenTargets.push_back( "Intens" );
listenTargets.push_back("Tilt");
//etc 

Then you can do:


for (int i = 0; i < listenTargets.size(); i++) {
    if( ofIsStringInString( incomingOSC, listenTargets[i] ){
         cout << i <<  "  "  << incomingOSC  << endl;
         //assuming a format like: "Intens [20]"
         auto splitString = ofSplitString(incomingOSC, "["); //lets split the string into a vector with  "Intens " and "20]" 
         if( splitString.size() > 1 ){ 
             auto numAsString = splitString[1];  //now get rid of the "]" 
             ofStringReplace(numAsString, "]");
             int num = ofToInt( numAsString );  //this should be the number in the [ ] 
             break; // stop search as we have a match 
         }  
    }
}