Function Like Visual Basics Mid.

I am trying to pull data out of the center of strings, I can so this easily in VB with the Mid function.

Example:

' Creates text string. 
Dim TestString As String = "Mid Function Demo" 
' Returns "Mid". 
Dim FirstWord As String = Mid(TestString, 1, 3)
' Returns "Demo". 
Dim LastWord As String = Mid(TestString, 14, 4)
' Returns "Function Demo". 
Dim MidWords As String = Mid(TestString, 5)

Is there something like this in Arduinio? If not what approach might you guys/gals recommend?

That can certainly be done. Can you lay out for us how it should act instead of just the vb demo? I can't figure out what the number arguments are supposed to do.

Probably the easiest thing would be to use strtok to split the string on spaces and then you can find the word you're looking for.

If you just want to split it on a particular position that can be done with normal array math and doesn't need any extra functions.

i'll jump in (being a VB programmer myself)

result = mid(string, start, length)

so

result = mid("The Cat in the Hat", 5,4)

would result in "Cat "

Oh well that's easy, how about strncpy?

char* aString = "The Cat in the Hat";

char[5] thePart;

strncpy(thePart, aString + 4, 4);

// thePart now contains "Cat "

KrisKasprzak,

Thanks for the better explanation!

Delta_G

That is exactly what I was looking for. I may have missed it but I never saw strncpy on the reference page. I guess it cant be all inclusive though :P. Thanks for the help!

Best Regards,
Paul

pguerra75:
KrisKasprzak,

Thanks for the better explanation!

Delta_G

That is exactly what I was looking for. I may have missed it but I never saw strncpy on the reference page. I guess it cant be all inclusive though :P. Thanks for the help!

Best Regards,
Paul

The Arduino reference is not exhaustive. It just gives the basic basics and the Arduino specific library stuff. Go look at C++ references and you'll find a LOT more stuff you can do.

Here's a better reference for the C libraries that exist "behind" the Arduino reference: <cstdlib> (stdlib.h) - C++ Reference

LOTS of useful functions there.

Delta_G:
Oh well that's easy, how about strncpy?

char* aString = "The Cat in the Hat";

char[5] thePart;

strncpy(thePart, aString + 4, 4);

// thePart now contains "Cat "

There is a possible subtle bug hidden in that code that will come bite you

See it? :slight_smile:

I don't see. Do I need to add a null on it?

You do :wink:

char * strncpy ( char * destination, const char * source, size_t num );

strncpy copies the first num characters of source to destination. If the end of the source C string (which is signaled by a null-character) is found before num characters have been copied, destination is padded with zeros until a total of num characters have been written to it.

No null-character is implicitly appended at the end of destination if source is longer than num. Thus, in this case, destination shall not be considered a null terminated C string (reading it as such would overflow).

Thanks J-M-L,

I was able to catch that after reading the reference.

Also
char[5] thePart;
should be
char thePart[5];

thanks again for every ones help!!

pguerra75:
Thanks J-M-L,

I was able to catch that after reading the reference.

Also
char[5] thePart;
should be
char thePart[5];

thanks again for every ones help!!

Yes it should. I should really stop trying to write code from a telephone.

Delta_G:
Yes it should. I should really stop trying to write code from a telephone.

Doing too much C#, do you :smiley: