-->
Page 1 of 2

String.substring() not working

PostPosted: Sat Sep 26, 2015 10:34 pm
by brutzler
Hi, anyone tested the "String.substring()" function? I do not get it working. "String.replace" is fine. Want to eleminate redundate spaces of HTML-code in a string before sending:
Code: Select all while (html_index.substring(0) == "  ") {
   html_index.replace("  "," ");
 }

Re: String.substring() not working

PostPosted: Sun Sep 27, 2015 6:42 am
by eduperez
I honestly think that the documentation for the String.substring() method could hardly be worse...

String.substring() does not "looks for a given substring from the position given to the end of the string"; it cuts a part of the string and lets you do whatever you want with that part. So, someString.substring(0) returns exactly the same as someString, because "someString.substring(0)" means "cut from the first character of someString, up to the end of someString".

In your code, (html_index.substring(0) == " ") is only true when html_index is exactly " "; far from what you need.

Re: String.substring() not working

PostPosted: Mon Sep 28, 2015 3:40 am
by brutzler
OK,

sounds like I have to use strstr()

Re: String.substring() not working

PostPosted: Mon Sep 28, 2015 9:37 am
by JohnSL
brutzler wrote:sounds like I have to use strstr()


You should be able to use indexOf:

https://www.arduino.cc/en/Reference/StringIndexOf

-- John