Add a single character inside a string!?

Hello. I'm with this problem. I have a string. Let's say it's a word with 3 characters: "123" in this case. Then i want to add another character in between somewhere. In this case i want to add "." between "2" and "3" so the word becomes "12.3". I know how to add strings before and after another string but not in between. And i don't know how to split a string so i can use this. Logically there is a function to add directly a character.

Any help?

Thanks!

This may be hackish but it works on my computer.

char s[10] = "1245";
memmove(s+3, s+2, strlen(s+2)*sizeof(s[0]));
s[2] = '3';

This works but i cannot implement it because my string variables are of string type and not char array. I've tried to fix it but i can't make it work!

This works but i cannot implement it because my string variables are of string type and not char array.

If you are using a String, rather than a string, why did you ask about strings?

If you are using a string, which is a NULL terminated array of chars, what is the problem?

If you are using something else, where is your code?

I'm really really new to this and i don't understand what you are talking about:/

  String s1;
  s1 = String(temp) + char(0xdf) + "C";
  lcd.setCursor(0,0);
  lcd.print(s1);

Basically temp comes as a 3-digit integer, example 221. And i want to display 22.1oC. I've done using a float instead of int, but all the conversion after makes the sketch a lot bigger.

Thanks

I'm really really new to this and i don't understand what you are talking about:/

Failing to use your shift key when it is needed is a big part of your problem.

A string is a NULL terminated array of chars. A String is a class that wastes memory. Which are you using?

St0RM53:
I'm really really new to this and i don't understand what you are talking about:/

  String s1;

s1 = String(temp) + char(0xdf) + "C";
  lcd.setCursor(0,0);
  lcd.print(s1);




Basically temp comes as a 3-digit integer, example 221. And i want to display 22.1oC. I've done using a float instead of int, but all the conversion after makes the sketch a lot bigger.

don't use a floating point number (which is stored in scientific notation) when dealing with fixed-point numbers (such as when you know you'll have exactly one digit after the decimal point). You're doing it right using the int rather than float.

You can split up your number before turning it into a String:

void setup() {
    Serial.begin(115200);
    int num = 221;
    String s = String(num/10) + "." + String(num%10);
    Serial.println(s);
}
void loop() {}

Or you can split up the String and put the period in the middle:

void setup() {
    Serial.begin(115200);

    int num = 221;
    String s1 = String(num);
    String s2 = s1.substring(0,s1.length()-1) + "." + s1.substring(s1.length()-1);

    Serial.println(s2);
}
void loop() {}

The first way is going to be much more efficient but if you already have a String...

1 Like

Thanks so much! It works! How can i test this though? How can i know which operation can be more efficient?

St0RM53:
Thanks so much! It works! How can i test this though? How can i know which operation can be more efficient?

Well efficient can refer to program space, RAM, or time. Program space is easy since arduino prints it out for you every time. RAM is tougher, but you can find a freeRAM function floating around the forums that should help. For time, you can run the same thing a million times (make sure the compiler doesn't optimize it out) and time the whole thing (run micros() at the beginning and the end and compare them)

However, since in both instances you are concatenating three strings as a final step, that will take the same amount of time. And I'm certain that a division and a modulo operator is going to take less time than two substring calls.

I don't think it would be obvious to a Newbie that String and string are different. How is s/he to know you haven't made a simple typo. A little more explanation would have been kinder.

...R

PaulS:

I'm really really new to this and i don't understand what you are talking about:/

Failing to use your shift key when it is needed is a big part of your problem.

A string is a NULL terminated array of chars. A String is a class that wastes memory. Which are you using?

I don't think it would be obvious to a Newbie that String and string are different.

Well, then, we'll just have to agree to disagree.

How is s/he to know you haven't made a simple typo.

I have always made it clear that string and String are different things. I have always jumped on people that assume that they are the same thing.

Jumping may be therapeutic for you but I doubt it is for the newbie. :slight_smile:

...R

PaulS:
I have always made it clear that string and String are different things. I have always jumped on people that assume that they are the same thing.

How else can we get people to distinguish between the radically different string and String types? I'm getting pretty tired of people that are too lazy to use the shift key.

If i knew there was a difference to it..Sorry. I'm new in C language.

A lot of the simpler languages aren't case-sensitive, unfortunately.
It's an easy mistake to make.

Will