Go Down

Topic: Add a single character inside a string!? (Read 959 times) previous topic - next topic

St0RM53

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!

WizenedEE

This may be hackish but it works on my computer.
Code: [Select]
char s[10] = "1245";
memmove(s+3, s+2, strlen(s+2)*sizeof(s[0]));
s[2] = '3';

St0RM53

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!

PaulS

Quote
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?

St0RM53

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


Code: [Select]
 
  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

Go Up