Pages: [1]   Go Down
Author Topic: Add a single character inside a string!?  (Read 860 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
 
  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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Code:
 
  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:
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:
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...
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Tesla Member
***
Karma: 121
Posts: 6990
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

UK
Offline Offline
Tesla Member
***
Karma: 121
Posts: 6990
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 611
Posts: 49101
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 14
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 49
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Will
Logged

Pages: [1]   Go Up
Jump to: