Any simple way to split a string to an array of string ?

Hello :slight_smile:

I'm trying to get an array of strings from a string by using delimiters.

I know how to do it in VB:

Dim str as String = "I LOVE ARDUINO"
Dim parts as String() = str.Split(" ")

Where, parts(0) is 'I', parts(1) is 'LOVE' and parts(2) is 'ARDUINO'

I've been searching for hours but I cant find anything :confused:

Hone someone can help :slight_smile:

I've been searching for hours but I cant find anything :confused:

Not true. You've already posted code that uses strtok() to parse the string. Storing the tokens in an array is simple, IF you know how many tokens, maximum, you need to save.

The problem is I DONT know how many tokens I'm going to have :confused:

The problem is I DONT know how many tokens I'm going to have :confused:

Where is the data you are parsing coming from? Can you impose a reasonable limit on the number of tokens?

The data is from a command line, and in the future, from a script, so it has to accept any number of tokens

so it has to accept any number of tokens

Really? It needs to be able to accept 34543315411651546 tokens? Where are you planning to store the data for that many tokens?

You REALLY need to think about reality here. Wanting to store an unlimited amount of character data is one thing. Reality is another.

Not that many, but I mean, It needs to accept how many tokens there are in the file or the command

alexandrerouma:
Not that many, but I mean, It needs to accept how many tokens there are in the file or the command

So, how many characters in the command? You need to be able to store that amount of data.

Perhaps an explanation of your program is in order. Why are you sending the commands to the Arduino? Why do you need to store the tokens (as character data)?

An example of the data would be in order, too.

I'm make an 'Operating System' for the arduino that will be able to execute Lua scripts.

It works entirely through serial using PuTTY as it's terminal.

It will have a shell, a user system with permissions, internet access and store files onto an SD Card.

Here is the code: GitHub - AlexandreRouma/ArduinOS: Serial OS For any Arduino

I just took a look at your ShowHelp function.
If you're trying to squeeze an OS onto a RAM-deficient architecture, you may want to consider better use of PROGMEM

Thanks for the suggestion :slight_smile:

I would strongly suggest the OP really needs to do some serious reading (and there are literally thousands of references on this topic) on HOW to do parsing properly before proceeding. Doing string compares, as his current code does, is very resource-intensive, and will very quickly become completely unmanageable on a small-memory system. There is virtually NEVER a need to break an entire arbitrary-length command into individual tokens all at one time. That is the very heart of actual parsers - break down the command quickly, efficiently, and with a minimum of resources. Does a c++ compiler have to break an entire program down into tokens before it can start figuring out what it all means? The answer is no. It rarely has to look at more than a very few tokens at any one time. Parsing, at its simplest, consists of two basic, and totally independent, steps: lexical analysis (i.e. - recognizing and extracting valid tokens), and parsing (i.e - recognizing and enforcing the language syntax). Trying to do both in a single operation is appropriate only for the most trivial of languages, and for all others very quickly becomes totally unmanageable.

Regards,
Ray L.