How to get a char out of a string?

Hello guys

I've been trying to figure out how to pick the char placed in a specific position of a string.

For example: my string is declared as "String Name = {"Ciro Bruno"};" and I want to attribute the content of its 7th position to a char declared as "char letter7;". What function should I use?

I've already been through:

  1. charAt() - Arduino Reference
    trying "letter7 = Name [7];"

and

  1. [] - Arduino Reference
    trying "Name.charAt (7);"

but any of these resources has worked!

What else to try?

Your variableName is an array of Strings, not a single String.

Use of String (capital S) is not advisable in memory constrained systems like an Uno or Mega. You might run into difficult to find runtime errors as it can leave holes in your memory. Rather use nul-terminated character arrays.

char myString[] = "Ciro Bruno";
void setup() {
  Serial.begin(9600);
}
void loop() {
  for (byte i = 0; i <= 9; i++) {
    char c = myString[i];
    Serial.println(c);
  }
  for ( ; ; ) {}
}

Works.

if you are using

String Name[] = {"Ciro Bruno"};

You are declaring an array of type String with 1 element. You probably want

String Name = "Ciro Bruno";

and then you can use

Name.charAt(7)

to get the 7th element

char myUsername[] = "Ciro Bruno";
void setup() {
  Serial.begin(9600);
  for (int i = 0; i < strlen (myUsername); i++) {
    char c = myUsername[i];
    Serial.println(c);
  }
  Serial.println();
}
void loop() {
}

Thank you. I've got it right now.

blh64: Yes, the brackets declare an "array of...". So "String Name " becomes an array of an array of chars. I won't forget it!

Perehama and TolpuddleSartre: Instead of containing all characters in a single string, a set of chars can be individually handled. Good resource!

sterretje: Ok about the array of strings. It's clear enough now. But, please could you explain the matter of capital S String?

sterretje again: You mean "\n" at the end of each string would help saving memory in a strings array? In which context? In an array of random length strings?

Thanks.
Regards,
Ciro

sterretje again: You mean "\n" at the end of each string would help saving memory in a strings array? In which context? In an array of random length strings?

No, '\n' is a newline character.
'\0' is a null character, used to terminate C strings.

Imagine you have two strings

String a = "Hello";
String b = "Hi";

The String class internally uses a pointer to a c-string. Let's assume that that pointer in a points at location 100 and the pointer in b points at 100 + 5 + 1 (106). In memory

+----------+--------------------+------------------+
| variable | text               | c-string address |
+----------+--------------------+------------------+
| String a | Hello              |  100             |
+----------+--------------------+------------------+
| String b | Hi                 |  106             |
+----------+---------------------------------------+

Now you add something to a; e.g.

a+=" world";

Because the c-string of a can't grow without overwriting the c-string of b, a new c-string for a is created after the c-string for b at 106 + 2 + 1 (109). You now have lost 6 bytes.

In memory

+----------+--------------------+------------------+
| variable | text               | c-string address |
+----------+--------------------+------------------+
|          |                    |  100             | this is now not used
+----------+--------------------+------------------+
| String b | Hi                 |  106             |
+----------+---------------------------------------+
| String a | Hello world        |  109             |
+----------+--------------------+------------------+

Next you can add something to b; again no space for the c-string to grow so it will be placed after the 'new' c-string of a.

Repeat it a number of times and you have no memory left. There might be a time where the unused memory is re-used, I haven't tested that; this might be the case when a modified String fits in the free space.

Demo code to demonstrate the principle; you need to modify the WString class for it to work.