String 'object' or not?

I have been advised, by several people, not to use the string object unless absolutely necessary.
I would also like to learn to do things for myself, when practical, so it seems like a good opportunity to learn.

I wrote some code recently that I just couldn't get to work, almost certainly because I was doing it incorrectly, using char.
Replacing all my char's with String fixed the problem but I suspect at a high Ram / Time cost.

Can someone help me with string handling, I have some basic functions to perform...

  1. Assign a value to a constant char, the string is in a struct so I believe I need the * operator to make it behave like a pointer (I think)

  2. Assign a value to a char dynamically, I am reading it from an SD card char by char.

  3. Compare two char's, the two above, my function accepts 1 as an argument and loops through an array of struct's and finds a match.

  4. This a little embarrassing :blush: serial.print a char - Sorry but it wouldn't work, although that may have been something I was doing with it earlier.

I can post code but it is only a short sketch doing the things above, just whilst I learn how to go about it.
most of it is serial prints fro debugging and it is all 'String's now anyway.

The array of structs is working and I don't need help with loops or basic program structure, its just the char's I cant get my head around.

Thanks
Al

Yep, post your code. Using char is simple enough.

Replacing all my char’s with String fixed the problem but I suspect at a high Ram / Time cost.

If, by fixed, you mean masked, I’ll accept that. The String class bring other (not entirely the fault of the String class) problems that have simply not yet bitten your derriere.

  1. Assign a value to a constant char, the string is in a struct so I believe I need the * operator to make it behave like a pointer (I think)

The strcat() function can do that.

char conStg[24];

conStg[0] = '\0'; // "Empty" the buffer
strcat(conStg, "Some constant string");
  1. Assign a value to a char dynamically, I am reading it from an SD card char by char.
char buffer[80];
byte index = 0;

while(file.available())
{
   if(index < 79)
   {
      buffer[index++] = file.read();
      buffer[index] = '\0'; // Keep it NULL terminated
   }
}
  1. Compare two char's, the two above, my function accepts 1 as an argument and loops through an array of struct’s and finds a match.
if(strcmp(conStg, buffer) == 0)
{
   // The contents match
}
  1. This a little embarrassing smiley-red serial.print a char - Sorry but it wouldn’t work, although that may have been something I was doing with it earlier.

This is #4, isn’t it?

Serial.print("conStg = [");
Serial.print(conStg);
Serial.println("]");

This will print

conStg = [Some constant string]

on one line and move the cursor to the start of the next line.

Edit: Added the missing the " in the snippet under #4. Thanks to rockwallaby for catching that.

#4 Yep … Hay I never said I could count.

Great answers thanks.

The serial.print thing must have been due to what I would have been trying to do with conStg in the first place, that bit I do get.

These I had no clue about,
strcmp > String Compare right?
strcat > Is this actually concatenate which is why you empty be string first

If it is concatenate would the following be valid? How is the terminating character handled?

conStg[0] = '\0';

strcat(conStg, "Some ");
strcat(conStg, "constant ");
strcat(conStg, "string");                    // conStg now contains "Some constant string" and a terminating null character

The empty buffer line …
Will that work on a char that already has contents reducing the size of the array? or is it only valid when initialising the variable?

Off topic but its my thread … ]:smiley: Are there function that will do this kind of stuff for none char arrays?

These are C functions right, is there list of string functions anywhere?
I have been trying to look things up for myself but my lack of basic C skills is hampering my ability to identify when I have found something useful.
It also doest help that I have realised now that I done even know what I need.

I am about to go and rebuild my little test script without using String … THANKS

strcmp > String Compare right?

Yes. As google no doubt told you. Or would have, had you asked.

strcat > Is this actually concatenate which is why you empty be string first

Yes and yes, though it is not always necessary to empty the string first, if you really do want to concatenate two strings.

If it is concatenate would the following be valid? How is the terminating character handled?

Yes. The strcat() function overwrites the existing NULL, and adds a new one at the end of the resulting string.

These are C functions right, is there list of string functions anywhere?

Yes, they are. I have a book called Microsoft C Bible that I grab whenever I want to check (for) a C function. But, any C book/tutorial should have plenty of string functions described/listed. And, of course, google is a good resource.

The empty buffer line ...
Will that work on a char that already has contents reducing the size of the array?

There are two "sizes". The number of characters in the string (i.e. the index of the NULL) and the size of the array (how many elements it can hold). Moving the NULL changes the first. The second is a constant.

It also doest help that I have realised now that I done even know what I need.

Been there, done that, got the t-shirt.

There are two "sizes". The number of characters in the string (i.e. the index of the NULL) and the size of the array (how many elements it can hold). Moving the NULL changes the first. The second is a constant.

So sizeof() would always return the size of the array not the number of characters.

I guess I will go Googling for string functions and see if I can learn something, it will be an interesting exersize I may find a function comparable to Len() which returns the index of the NULL, who knows.

I guess the next logical question would be about resizing arrays ... I will give Google another go I am beginning to recognise things a little now perhaps it will make more sense that it did over the weekend.

All the help is very much appreciated thanks.

I may find a function comparable to Len() which returns the index of the NULL, who knows.

Ooh. I do. strlen().

This link seems like it should help:

http://www.cplusplus.com/reference/clibrary/cstring/

strlen() ... I should have guessed :.

That link is great thanks.
I had found the site but didn't know what to look for exactly.

The problem with looking for stuff on/in a reference is that there always options and if you find one you have no way to know that there is an alternative.
Well unless the reference mentions it.

Once again thanks folks.

Al