I need to separate numbers from the text where my string of numbers will always be of length 8:

string text = "77953505HELLO THIS IS A MESSAGE"; //my string of numbers will always be of length 8

My result

string number = "77953505"; //my string of numbers will always be of length 8
string message = "HELLO THIS IS A MESSAGE";

What operation should I use?

this is the job I'm doing

void setup() {

void loop() {
  if (Serial.available() > 0) {

    String Texto = String(Serial.read());

    String number = Texto.substring(0, 7);
    String message = Texto.substring(8);


help me!


Depends, and probably better to use C strings:

  1. If you know the number is always the same amount of characters, use the C function strncpy.

  2. You can also split on a delimiter (char), with strtok, but in your case this is not possible since you don't know the delimiter character.

  3. If the number has a variable length, start copying character by character through the string, until you find a character that gives false for the isdigit condition and copy the remainder (with strncpy) to the other string.

Update after the question's edit

Since the number of text to be copied is know, strncpy can be used with the following prototype:

char *strncpy(char *dest, const char *src, size_t n)

In your case put the text in a normal text string:

char text[80]; /* Assuming max length of text */

Copy it where needed:

strcpy(text, "77953505HELLO THIS IS A MESSAGE");

Note: it can be done in once like this:

char text[] = "77953505HELLO THIS IS A MESSAGE";

Than you can copy the items by:

char number[9]; // Add 1 for \0
char message[81]; // Assuming max length of message;

strncpy(number, text, 8);
strncpy(message, &(text[8]), strlen(text) - 8);

(Note not checked with compiler).

Improvements: use instead of 8 a define e.g.


char number[NUMBER_LENGTH + 1];

strncpy(number, text, NUMBER_LENGTH);
strncpy(message, &(text[8]), strlen(text) - NUMBER_LENGTH);

You can do similar for MESSAGE LENGTH


Also, another improvement is not to copy the message, but just set the pointer to the correct place:

char* message = &(text[8]); 

message will point to "HELLO THIS IS A MESSAGE"

like below

|        |
+-- text |
         +-- message

In an Arduino 80 bytes can be a lot of memory, using two buffers cost 160 bytes, and this way you save at least one buffer.

  • 1
    i edited my question – R. Fernandez Apr 9 '18 at 17:38
  • So in this case you can use strncat; I will edit my answer – Michel Keijzers Apr 9 '18 at 19:15
  • I changed strncat by strncpy and added some improvements. – Michel Keijzers Apr 9 '18 at 19:26
  • look at my edition – R. Fernandez Apr 11 '18 at 13:53
  • what output you get? I don't see a problem so far. – Michel Keijzers Apr 11 '18 at 14:02

For a fixed length substring the simple arduino-ish way would be to use String.substring(left_pos, right_pos)

String number = text.substring(0, 7);
String message = test.substring(8); // right_pos is optional

This is not necessarily the best option depending on how you intend to use the resulting substring. Memory is allocated and the content is copied. Requiring at least double the memory (plus some overhead). If memory is tight you may not want to create a String but use char* instead.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.