String - substring


I need to extract 2 substrings from a string: this is the String "ssid=aaaaa&passwd=bbbbbb"

Basically I want to get the aaaaa and bbbbb values from the String to 2 different Strings.

This is the piece of code I wrote:

int commaIndex = req.indexOf('=');
    int secondCommaIndex = req.indexOf('&');
    int thirdCommaIndex = req.indexOf('=', commaIndex+1);
    String firstValue = req.substring(0, commaIndex);
    String secondValue = req.substring(commaIndex+1, secondCommaIndex);
    String thirdValue = req.substring(secondCommaIndex+1,thirdCommaIndex);        // To the end of 
   the string 
  String fourthValue = req.substring(thirdCommaIndex+1);                   // To the end of the string

This is the output on the Serial monitor:

String2.: aaaaaaa
String3.: passwd
String4.: bbbbbbb HTTP/1.1

How can I remove the HTTP/1.1 from String4?


This is the output on the Serial monitor:

From code you failed to show...

There IS a space before the HTTP part. Make use of that fact.

First break the request into three parts using the space delimiter

  1. method (GET, POST, etc)
  2. URL
  3. Version

Then break the URL into its parts using the question-mark delimiter (no-one uses the semicolon thing)

  1. path
  2. query string

Then decode the parts of the URL using URL percent encoding.

Then decode the querystring according to x-www-form-urlencoded format.

Happily, this format always expands each character, so you can decode the string in-place.

I believe that this means that if there is a percent in the query string, then it has to get double-encoded. That is, if someone puts "10%" in a form input element (say, "rate"), your app will receive /myform?rate=10%2525 as the url.