How to detach/grab last word in a string?

Nishant's picture


Joined: Sep 12, 2018

Replies: 0
How to detach/grab last word in a string?
August 13, 2019 - 2:55pm

i am sending one string from HTTP to ESP8266 WiFi. here i need to add acknowledgment for the http server if i have received the data successfully. basically i want to add a word in the last of string which will act like an identifier .

Suppose i am sending one message from HTTP such as "I am message from HTTP server OK".

Here the last word "OK" will act as an identifier to send back the acknowledgement. 

So is there any short way to detack the "OK" from the Complete string "I am message from HTTP server OK".

After successfully detaching i will use it to reply.

Suppose in arduino,

String received = "";

if(received =="OK"){

reply back "Received" to server;


how to do this?

AISHA's picture


Joined: Sep 07, 2017

Replies: 256

Hi Nishant, welcome to circuitdigest Forums....

What you are asking for can be done in many way, it depends on your payload string. If the string is fixed with very few variables like you either get

 "I am message from HTTP server OK" OR  "I am message from HTTP server FAIL"

In this case you can use the substring option available with Arduino. Because your information always starts at the 31st position for your string (count chars from zero on above string). This is the most easy because you can simply use information = payload.substring(31,35); where 31 to 35 is the position of char you are intrested in 

If this is not the case then you have to look for an identifier after which you can start looking for meaningfull data like for example

"Disctance covered: 234" OR "Speed: 15"

In such a case you have to read every char and discard it till you read the identifier in this case ":". Once you have read your identifier you can start reading the chars that are of valueable information.

If both the above methods fails and if you are sure that the required information is always at the end of the string, then you can use "/n" as you identifier, because every string has to end with "/n". In this method you have to save the complete string and then discard the unwanted text after you have noticed a "/n"


Hope it helps!!