Go Down

Topic: string functions (Read 748 times) previous topic - next topic

k3pto

Is there somewhere that documents all of the available string functions for the Arduino?  I have  tried searching the Forum as well as the Arduino Reference and cannot locate anything.

ToddL1962

If you are speaking of the String class then it is documented here and not sure why you couldn't find it:

String

if you are speaking of C string functions then they are available in any decent C/C++ reference.

econjack

Many of us here prefer to use char arrays instead of the String class because there are some issues with the class. You can find a fairly complete list of the string processing functions here. Note that some of the mem*() functions duplicate the stgr*() functions. Generally, they serve the same purpose, but the mem*() functions have the training wheels removed (i.e., no runtime checks).

johnwasser

What is a 'string':
https://www.arduino.cc/reference/en/language/variables/data-types/string/


The standard string functions defined in the AVR processor compiler library:
https://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

This covers conversions from numbers to strings and strings to numbers:
https://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp
See who has no social life: https://forum.arduino.cc/index.php?action=stats :)

UKHeliBob

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

SteveMann

You will hear a lot about the evil String class. This explains why you should avoid using Strings.
If you are fairly new to Arduino and C++, learn the right way from the start and you won't have to unlearn Strings when an ambitious, memory critical project crashes for unexplained reasons.
Fritzing pictures are NOT schematics. I don't speak Fritzing.
Click on Add Karma if I helped you.
Please do not ask for help by PM. I will not respond. If you need help, post a question on the appropriate forum.

drmpf

#6
Apr 08, 2021, 03:10 am Last Edit: Apr 08, 2021, 03:16 am by drmpf
Quote
You will hear a lot about the evil String class.
Which is mostly wrong
See my tutoral on Taming Arduino Strings for how to use Arduino Strings without problems.

The evil strings reference suggests using c-string methods instead which are much more dangerous and very pone to coder errors than Arduino Strings.
You don't need a ambitious project to fall in to a hole when you use c-string methods. See this post for an example of the problems you will face https://forum.arduino.cc/index.php?topic=730965.0 using char[] and low level c-string methods

If you want to use char[], try my SafeString library, (detailed tutorial here) which adds all the checks that should be added to c-string methods to make them safe, but which no one seems to add.

lastchancename

@drmpf, you're. obviously an intelligent, competent programmer, but your determination to waste newbie resources on the String class with tiny embedded processors is baffling.

Strings are great, and you obviously have come to understand what many of us learned thirty years ago - without the painful consequences.  I understand times have moved on since then, just as program complexity has. using that enhanced capability to mask errors and weaknesses is not a good starting point.

Beginners SHOULD get to understand c-strings, hopefully that will let the use the String classes when relevant.
Experienced responders have a nose for laziness, (they were beginners once)... Sure, there are trolls, chest-beaters, and pretenders - but the help you'll get here is about as good as it gets - if you try to help youself!.

drmpf

Arduino CC decided that Strings were what beginners should used for text manipulations so the Arduino Language Reference uses them and completely ignores low level c-string methods.

On the UNO, Strings are virtually bullet proof.  So no wonder Arduino CC chose them to be used on their first tiny embedded processor.

Recent boards have much more memory, so the argument on the basis of wasted resources is even less valid.
c-string methods are just too error prone as the referenced post shows.



westfw

Quote
Note that some of the mem*() functions duplicate the stgr*() functions. Generally, they serve the same purpose, but the mem*() functions have the training wheels removed (i.e., no runtime checks).
Um.  The str* functions have very few runtime checks either, thus leading to many a buffer-overflow bug.
They operate on what C calls "strings", which are null-terminated byte arrays.  The mem* functions don't care about nulls; they're strictly length-based (which allows them to be used on data that isn't text; memcpy() can be used to copy arbitrary "things."  But it also means that something like memcat() doesn't exist (or make any sense.)
 

UKHeliBob

Quote
the Arduino Language Reference uses them and completely ignores low level c-string methods.
The clue is in the name.  It is the Arduino Language Reference
It is not a C/C++ Language Reference
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

drmpf

Yes the clue is in this forum name.  This is the Arduino Forum
It is not a C/C++ forum

UKHeliBob

Quote
This is the Arduino Forum
and your point is what exactly ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

drmpf

Arduino Strings are safe and usable and this is an Arduino Forum, so why descending into low level c-string methods for solutions that would be more appropriate as answers on a C/C++ forum.

J-M-L

#14
Apr 08, 2021, 10:44 am Last Edit: Apr 08, 2021, 11:10 am by J-M-L
Arduino Strings are safe and usable
So why do you keep pointing at your SafeString library and your tutorial on "Taming Arduino Strings for how to use Arduino Strings without problems"?

if they are OK why would they need taming?
if they are OK then your library should not be discussed in the Arduino forum either, right?

also, remind us what did you use to write your SafeString library? cStrings or the String class? (answer = cString, just like the String class.... so if they are good for the Arduino team and you, why shouldn't they be OK to learn about by the rest of the community?)

The point is that making sound decisions based on facts is part of a programer's life. Understanding how to deal with buffers, bounds and checking for limit cases and making decisions when bad things happen is an integral part of learning how to master scarce memory and the C/C++ language. And you still have to do so with your "safestring" library as you don't have dynamic storage expansion anyway.

Exposing newbies to this reality is important and should not be hidden, it's a learning opportunity.  Then they can decide for themselves what they want to use. If it's a 20 lines piece of code and 3 minutes show and tell throw away project for school and they don't care about programming, then it might not require all that time investment and it's OK to go for the String class, but at least it's a conscious decision.

Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

Go Up