Go Down

Topic: Add a single character inside a string!? (Read 963 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

PaulS

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

WizenedEE


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.

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

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

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...

St0RM53

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

WizenedEE


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.

Robin2

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


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

PaulS

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

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

Robin2

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

...R



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.

PaulS

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.

St0RM53

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

cwrose

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

Will

Go Up