0

I have an array of Strings in my code:

String BitSequence [257];

Each string will have 23~64 characters. At one point in my code, I have to use "myString.remove()" command and I don't want to apply any changes to my original strings. I need to make a copy and perform my commands on the copied string. I did:

MycopyString = BitSequence [1];

but it doesn't work. What tricks do I need to do here to make a copy?

9
  • What does "it doesn't work" mean? What did you expect and what actually happened? – chrisl Apr 4 '20 at 8:17
  • don't use String class at all. majenko.co.uk/blog/evils-arduino-strings – Juraj Apr 4 '20 at 9:01
  • try strcpy...... – Vaibhav Apr 4 '20 at 10:00
  • @chrisl "What does "it doesn't work" mean? " resulting String is empty. – Afshin Apr 4 '20 at 17:29
  • The Arduino Nano does not have enough RAM to store 257 single-character Strings. – Edgar Bonet Apr 5 '20 at 20:03
0

Do not work with the String class on Arduino. That said what to do?
Use char arrays (what an individual String is internally). Then you work with pointers or pointers to pointers. Guessing from the fragments (what type is MycopyString?) you gave, you create a pointer (?) to the element in the array.
Working with String class has two disadvantages.

  • You have to look into the lib to see what a function really does and
  • by reserving and releasing memory it fractures on the long (most of the time short) run your heap causing your AVR/ESP to crash.

The possibility to create two dimensional arrays

i = 257;
char myTwoDimArray [i][64] = {'\0','\0'}// 64 ... 63 chars + 1 terminator(!) 

and then you operate with pointers either to the entry (i) or in the specified char array itself.

How to remove an entry You just write:

i = 23;
myTwoDimArray [i][0] = '\0';

by writing a zero terminator to the first index (starts with 0) of the 24th (!) element (again the index starts with 0) its deleted.

Commands to look for when operating with chars

strcpy (targetChar, charToAdd); // Initializes the targetChar starts at index 0
strcat (targetChar, charToAdd); // Appends to the targetChar starts at current index

converting numeric values to chars:

  char numBuffer [16] = {'\0'}; // Helper buffer for conversions

 itoa (myNumberValue,numBuffer,10);  // converts an integer to a base 10 (decimal) char
itoa (myNumberValue,numBuffer,2);  // converts an integer to a base 2 (binary) char
itoa (myNuberValue,numBuffer,16);  // converts an integer to a base 16 (hex) char

itoa initilizes a char array so we need the helper array:

strcat (targetChar, numBuffer);    

to convert float we use

dtostrf(floatVariable, StringLengthIncDecimalPoint, numVarsAfterDecimal, numBuffer);

to convert chars back to int use

int16_t myIntVar = atoi(numBuffer);

to convert chars back to floats use

float myFloatVar = atof(numBuffer, decimalsToShow); // using just atof(numBuffer)
gives you standard x.XX only 2 decimals

Hope this comprehensive intro to chars helps you for your current and future projects.

3
  • Ever checked the result of 64 * 257 ? @Afshin mentions an Arduino Nano. You're right: Strings are much worse, of course. – DataFiddler Apr 4 '20 at 10:52
  • Yes I checked but the OP probably not and chars will make him aware ;-) -hopefully – Codebreaker007 Apr 4 '20 at 11:41
  • Amazing explanation. It has been a week I started programming Arduinos for a project of mine, and I am kind naive. Thanks a lot. – Afshin Apr 4 '20 at 17:21
1

I don't want to apply any changes to my original strings

You should read about PROGMEM and/or the __FlashStringHelper ( F-Macro ) . Probably you can even handle that vast amount of constant texts on your Nano controller, which is meant to be a controller, not a text processing computer.

3
  • You are right! but I need to receive a large file and then process it. I found using string provides me the fastest communication bit rate, maybe I am wrong since I am just a new programmer! Also, Nano BLE has 256KB of ram so, I thought, this shouldn't be a problem. – Afshin Apr 4 '20 at 17:25
  • Arduino String is never fastest nor least memory consuming. OK, Nano is an atmega328-based Arduino Nano for me (2kB RAM) so my concern might not apply here. Sorry – DataFiddler Apr 7 '20 at 9:59
  • I had to clarify that I am using Nano 33 BLE. Sorry for confusion – Afshin Apr 8 '20 at 21:39
0

Thank you for all the helpful answers. I think also found a way to copy strings. Using "myString.substring(from,to)" I could resolve my problem. The command works on array strings as well.

mynewcopystring =mystring[2].substring(0,mystring[2].length());

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.