Is it possible to compare 2 string in an if then statement? and if so, how could I compare the 3rd word of each strand to each other? I am trying to compare 2 different strings which the Arduino will add to depending on which buttons are pressed, sort of like a bop it game. Any help is greatly appreciated!
Do you mean Strings (objects of the String library) or strings (zero terminated arrays of chars) ?
Please talk us through that sentence.
Do you maybe have some source code yet?
Best Regards
Frenik
the words that i would store in a string. i may convert it into numbers depending on if it is easier
Numbers are easier to compare.
is it possible to compare the 3rd number of 2 strings?
i read this, but i cant seem to find if i can read a specific part of a string.
I don't think anyone has a clear idea of what you are talking about. Please explain clearly, using actual examples of the "strings" and "numbers".
If you answer that question, you might get tailored help.
If you post a minimal example, you will get better help.
Check charAt() - Arduino Reference and substring() - Arduino Reference
Note that the incorrect use of String (capital S) can result in problems while your sketch is running.
Definitely yes.
First you need to parse the strings, find and retrieve 3rd word, convert it to integer and then compare it.
You can go on for another 50 posts with short worded generalised postings if you like to play a ping-pong-game of postings.
If you prefer to get a real solution as soon as possible you should post examples
How do your strings look like?
Examples:
String 1: "my moms car is red"
String 2: "my dads bike is green"
String 1: "526 1004 92 00012"
String 2: "983 725 2009 00451"
String 1: "34-MA-3 BJ2014 KL MMW"
String 2: "921K452;12;30;1998"
The solution will depened on how your string looks like
Do you have any code yet where the strings come from? If yes you should post this code as a code-section
best regards Stefan
More importantly the solution will depend on whether what you are comparing are Strings or strings or even possibly a mixture of both
@dronemaster278
Please post your code so that we can see what exactly you are comparing
I guess it will be good to explain the difference between them.
There are two fundamental different types of "variables" for storing a sequence of characters.
type 1: String with a CAPITAL "S"
type 2: string with lowercase "s"
A String is an "object" which has many "functions" inbuild but needs a lot of memory including eating up all RAM-memory over time causing very hard to find bugs when used in the wrong way.
A string (lowercase "s") is an array of char. Which uses completely different functions for scanning strings / compairing strings. Arrays of char have their own disadvantages as you have to take care of boundary-checking very very carefully to avoid corrupting your data.
A lot of users here don't like a third type of "storing character-sequences". But in my opinion this third option is very good for beginners for three reasons:
- it is safe to use. No possability for memory-corruption
- It offers almost the same comfort as String
- RAM-usage is fixed and will never change (like with String
This is called SafeString. The name is program. It is a library that you can install through the library-manager.
best regards Stefan
Example:
String1: A, B, C, D
String2: 1, 2, 3, 4
I am trying to compare the 3rd values, (c and 3), of each string.
Thank you, I do not have any code, as I am just trying to see if it is possible. I have posted above what the strings might look like.
Did you mean 'C'?
Either way, 'c' or 'C' is never going to be equal to 3 or '3'.
You need to be clearer.
this is existing code from another project, and i am wondering if it would be better to do it this way.
int current1[125]{ 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17, 17,
16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8,
7, 6, 5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2,
1, 0, 17, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17 };
int current2[125]{ 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17, 17, 16, 15,
14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6,
5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 17,
17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 17, 17, 17, 17, 17 };
I would try to compare the first digits from each string.