string functions

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.

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.

1 Like

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).

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

https://www.cplusplus.com/reference/cstring/

1 Like

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.

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.

@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.

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.

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.)

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

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

This is the Arduino Forum

and your point is what exactly ?

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.

drmpf:
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.

J-M-L++

@drmpf - as to not using c-strings because they are

dangerous and very pone to coder errors

I assume that we should also avoid the use of arrays of any kind for the same reason

From them “taming strings” page:

  1. Do not use the String + operator at all and avoid using the String(...) constructors as these create short lived temporary Strings.

Unfortunately, using “+” seems to be the most common use of Strings. Indeed, it’s presence is one of THE main features of Strings over the normal C functions. If you’re going to restrict yourself to “+=“, you might as well use strcat()

None of the comments above explain why, in the first instance, when answering an OP's question on this forum,
you would not use the built in safety and usability of Arduino Strings (or my SafeString library)

20 years ago C++ introduced Strings to overcome the severe reliability and coding issues arising using c-string methods.

Raw c-string methods are very fragile and are so prone to coder error that Microsoft has banned their use and text books have been written on why you should not use them.

Hardly the thing to recommend in preference to Arduino Strings on an Arduino Forum

This is the Arduino Forum and answers should be based, in the first instance, on the Arduino Language Reference unless there is a well defined and well explained reason.

The first answer #1 was adressing OP's question, then more input was provided (#2, #3, #4) on cString documentation.

The rest was an interesting digression this forum is famous for :slight_smile:

20 years ago C++ introduced Strings to overcome the severe reliability and coding issues arising using c-string methods. Raw c-string methods are very fragile

and yet neither the String class nor your library is using the C++ Strings... They use cStrings functions, isn't it?

Microsoft has banned their use

be complete in your statement: they listed functions that should be removed to reduce vulnerabilities as part of Security Development Lifecycle practices for modern computers (--> risk of security bugs).

They also explained that their replacement functions are not intrinsically safer and recommend to double check that the number Of Elements argument is no larger than the destination buffer size, which is what diligent programmer do in the first place when using cStrings (like you do in your library).

The takeaway point is that it's good to know cStrings, how to handle buffers and arrays, checking bounds etc. Then, equipped with this knowledge and based on your specifics, you can use the library you want.

None of the comments above explain why, in the first instance, when answering an OP's question on this forum,
you would not use the built in safety and usability of Arduino Strings (or my SafeString library)

Being pedantic, the OP did not ask about Strings, he/she asked about strings. For all we know they meant what they wrote

This is the Arduino Forum and answers should be based, in the first instance, on the Arduino Language Reference

Oh dear. There goes most of C and C++ then

unless there is a well defined and well explained reason.

such as those linked to from this topic

In practice I don't think that using Strings is such a bad thing when done correctly in the right environment. The problem is that the easy to use String functions lead to extensive use in the wrong way in the wrong environment. The fact that they can be used without thought of the consequences is both an advantage and a problem waiting to happen