How to return String from function

Hello. I can not get the value of string from the function. Could you help me, please? This function is to return part of the string between two dividers.

#include <Arduino.h>
void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
}


  String StrFind(String val, String from, String to){
    int fromStarts = val.indexOf(from);

    String sym;
    int f;

    int theEnd = 0;
    int theStart = 0;
    int k = val.length();

    for(int i=fromStarts-1; i<k; i++){
      theStart = i;
      sym = val.substring(i, i+1);
      if (sym==to) {
        break;
      }
        }
        //String b =
        return val.substring(fromStarts, theStart);
        delay(1000);
  }
  void loop(){
      String gh = StrFind("d:kiev.com-lolin-pswd,d:moskva.com-lolinmosq-pswd,d:perm.com-lolinprm-pswd,", "perm.com", ",");
  //  Serial.print(gh);
  }

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

In general you cannot return an array that is created as a local variable because it will no longer exist when the function in which it was defined terminates.

...R

Robin2: In general you cannot return an array that is created as a local variable because it will no longer exist when the function in which it was defined terminates.

...R

Yes, and... the usual simple solution to that problem is to pass a reference to an array that you have defined globally, as a parameter to the function.

aarg: Yes, and... the usual simple solution to that problem is to pass a reference to an array that you have defined globally, as a parameter to the function.

The array really only needs to be defined in the scope of the calling function.

gfvalvo: The array really only needs to be defined in the scope of the calling function.

Right, sorry. A lot of things would be awkward if that wasn't the case.

aarg: Right, sorry. A lot of things would be awkward if that wasn't the case.

Could you help me with the code please?

Check out the functions in the string.h library.

strstr() will search for a substring and return a pointer to it (if found, null otherwise). Use it to find your "from" value and the following "," from that point. Or, maybe better to use strchr() for your second search as it's only for a single character.

strncpy() can then copy the substring (using the pointers you've found) to the destination.

You'll have to handle exceptions like the search string (or char) not found. Also, be sure you have a null char ('\0') placed at the end of your destination string.

Finally, be sure your destination buffer (whose pointer you pass to your StrFind() function) is large enough to hold the substring you're searching for.

NavyGunner:
Arduino C; if you can even call it that,

in Real ‘C’ you can return and pass a string like this

string MyFunction(string Mystring)
{
string a = “Hello”;
return a;
}

because the string is returned from a function to use it elsewhere you can call it like
this

string YourString = MyFunction(“Goodby”);

YourString will now equal “Hello”;

it’s a real shame Aruino crippled C to the point where it is all but unusable.
who ever wrote the compiler needs to read Brian Kernighan and Dennis Ritchie’s book on C.

C doesn’t have string/class.
For C++, the variable a is still local, and still runs out of scope. The “effect” you observe here is: a is copied and YourString is initialized with it (assuming C++11).

So: OP’s code should work because he returns a local object.
@OP: what is your debug outputs?

Arduino C; if you can even call it that,

Arduino uses C++ otherwise you could not use Strings (note, capital S) at all so why your nonsense about "Arduino C" ?

Still nobody answered the OP's question.

In Pascal you can declare a function of type string and call that function as if it is a string. E.g. I made one called WhatTime. The statement: Writeln('It''s now '+WhatTime); produced something like "It's now exactly noon" or "It's now half past seven in the morning"

Can you do that in Arduino?

Still nobody answered the OP's question.

Welcome to 2020

What has your reply to this long dormant topic got to do with returning a String from a function ?