Split String into String array

前端 未结 6 1837
小蘑菇
小蘑菇 2020-12-31 08:14

I have been playing around with programming for arduino but today i\'ve come across a problem that i can\'t solve with my very limited C knowledge. Here\'s how it goes. I\'m

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-31 09:15

    I think your idea is a good start point. Here is a code that i use (to parse HTTP GET REST requests with an Ethernet shield).

    The idea is to use a while loop and lastIndexOf of and store the strings into an array (but your could do something else).

    "request" is the string you want to parse (for me it was called request because.. it was).

        int goOn = 1;
        int count = -1;
        int pos1;
        int pos2 = request.length();
    
        while( goOn == 1 ) {
            pos1 = request.lastIndexOf("/", pos2);
            pos2 = request.lastIndexOf("/", pos1 - 1);
    
            if( pos2 <= 0 ) goOn = 0;
    
            String tmp = request.substring(pos2 + 1, pos1);
    
            count++;
            params[count] = tmp;
    
            // Serial.println( params[count] );
    
            if( goOn != 1) break;
        }
        // At the end you can know how many items the array will have: count + 1 !
    

    I have used this code successfully, but i thing their is an encoding problem when i try to print params[x]... i'm alos a beginner so i don't master chars vs string...

    Hope it helps.

提交回复
热议问题