| View previous topic :: View next topic |
| Author |
Message |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jun 24, 2008 9:47 am Post subject: [Delphi - Help needed] Check if 2 strings are equal |
|
|
Hey, got a bit of a problem here.
I'm using an "if and" structure, and got some problems in that.
My var section:
| Code: | var
...
string1, string2: string; |
They're assignment:
| Code: | string1 := '';
string2 := 'test'; //Some random text |
My "if and" structure:
| Code: | if CONDITION1 and string1 <> string2 then
begin
...
end; |
My errors (On same line, after "and"):
| Code: | Incompatible types: 'String' and 'Integer'
Incompatible types: 'String' and 'Boolean' |
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Jun 24, 2008 10:16 am Post subject: |
|
|
"if" "and" are statements, and do it like:
| Code: | | if string1 = string2 then |
P.S - <> means !=, like if not hwnd = 0 then will be the same as if hwnd <> 0 ok ?
leave feedback
|
|
| Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jun 24, 2008 10:34 am Post subject: |
|
|
| Rot1 wrote: | "if" "and" are statements, and do it like:
| Code: | | if string1 = string2 then |
P.S - <> means !=, like if not hwnd = 0 then will be the same as if hwnd <> 0 ok ?
leave feedback |
Sorry about the statement/structure part.
I've tried this:
| Code: | | if length(string1) > 2 then if String1 = String2 then |
Doesn't work either.
|
|
| Back to top |
|
 |
tombana Master Cheater
Reputation: 2
Joined: 14 Jun 2007 Posts: 456 Location: The Netherlands
|
Posted: Tue Jun 24, 2008 10:59 am Post subject: |
|
|
| I don't know about Delphi, but in C++ you can't compare strings like that. You have to use a functions like lstrcmp.
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Tue Jun 24, 2008 12:08 pm Post subject: |
|
|
| Oh, your problem is simple. You are attempting to compare two variables of different types. You are comparing a string to an integer. That simply does not work. You have to make both the same type. In other words, use the "StrToInt" function on the string to convert it to an integer before comparing it with the 2. Your second error occurs, because you are testing a boolean variable(True vs. false) on a string as well. Convert the string to a boolean value, similarily, and you should not have problems.
|
|
| Back to top |
|
 |
Jani Grandmaster Cheater
Reputation: 2
Joined: 29 Dec 2006 Posts: 804
|
Posted: Tue Jun 24, 2008 12:25 pm Post subject: |
|
|
| tombana wrote: | | I don't know about Delphi, but in C++ you can't compare strings like that. You have to use a functions like lstrcmp. | Actually you can. Altho, you need to use == operator, not =. | Code: | #include <string>
std::string s1("asd");
std::string s2("as");
if( s1 == s2 ) { /* This won't be executed */; }
s2.append("d");
if( s1 == s2 ) { /* This will be executed */; } |
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Tue Jun 24, 2008 12:47 pm Post subject: |
|
|
Writing "if CONDITION1 and string1 <> string2 then" is like doing logical and between CONDITION1 and string1 and then check if the result equal to string2.
You should write it like "if CONDITION1 and (string1 <> string2) then".
|
|
| Back to top |
|
 |
DeletedUser14087 I post too much
Reputation: 2
Joined: 21 Jun 2006 Posts: 3069
|
Posted: Tue Jun 24, 2008 1:17 pm Post subject: |
|
|
| Moller wrote: | | Rot1 wrote: | "if" "and" are statements, and do it like:
| Code: | | if string1 = string2 then |
P.S - <> means !=, like if not hwnd = 0 then will be the same as if hwnd <> 0 ok ?
leave feedback |
Sorry about the statement/structure part.
I've tried this:
| Code: | | if length(string1) > 2 then if String1 = String2 then |
Doesn't work either. |
You're now checking of the length of the strings are equal, try:
if not string1<>string2 then result:=true; //return value is true if the strings are the same
oh yeah, it's like this:
| Code: | | if IntToStr(String1) = IntToStr(String2) then |
|
|
| Back to top |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Tue Jun 24, 2008 2:10 pm Post subject: |
|
|
Add parenthesis... This is how it should be:
| Code: |
if ((BOOLEAN condition) and (str1 = str2)) then
...
| [/code]
|
|
| Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Tue Jun 24, 2008 2:53 pm Post subject: |
|
|
Okay, I've gotten this far.
| Code: | | if ((length(temp) > 2) and (IntToStr(temp) = IntToStr(lastUser)) then |
Now the error is:
| Code: | | There is no overloaded version of 'IntToStr' that can be called with these arguments |
|
|
| Back to top |
|
 |
WafflesFTW Expert Cheater
Reputation: 0
Joined: 21 Mar 2008 Posts: 131
|
Posted: Tue Jun 24, 2008 3:26 pm Post subject: |
|
|
| StrToInt, not IntToStr
|
|
| Back to top |
|
 |
Dark Byte Site Admin
Reputation: 471
Joined: 09 May 2003 Posts: 25833 Location: The netherlands
|
Posted: Tue Jun 24, 2008 4:30 pm Post subject: |
|
|
| Code: |
if (length(temp) > 2) and (temp = lastUser) then
|
_________________
Do not ask me about online cheats. I don't know any and wont help finding them.
Like my help? Join me on Patreon so i can keep helping |
|
| Back to top |
|
 |
ups2000ups I post too much
Reputation: 0
Joined: 31 Jul 2006 Posts: 2471
|
Posted: Tue Jun 24, 2008 8:38 pm Post subject: |
|
|
well i dont know why all started to say inttostr (since it was never an integer)
just look what DarkByte sade
_________________
dont complain about my english...
1*1 = 2? |
|
| Back to top |
|
 |
NothingToShow Grandmaster Cheater Supreme
Reputation: 0
Joined: 11 Jul 2007 Posts: 1579
|
Posted: Wed Jun 25, 2008 1:04 am Post subject: |
|
|
I'm sure Dark Bytes method works, but it's not what I'm in need of.
I need to make sure that temp is not equal lastUser.
I'm very sorry if I've said it the other way around.
|
|
| Back to top |
|
 |
HolyBlah Master Cheater
Reputation: 2
Joined: 24 Aug 2007 Posts: 446
|
Posted: Wed Jun 25, 2008 1:06 am Post subject: |
|
|
| Code: | | if (length(temp) > 2) and (temp <> lastUser) then |
|
|
| Back to top |
|
 |
|