Jump to content




How can I compare Strings?


  • You cannot reply to this topic
9 replies to this topic

#1 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 04:26 AM

Hi all,
I want to compare two strings ^_^
I need It for my OS cuz It have two login systems (one with the http API (mysql) and one with user data on the computer). So, or the second oneI need to compare two SHA1 Hashs :)
how can I do that?


#2 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 22 April 2013 - 04:27 AM

if 'stringTest1' == 'stringText1' then
  print('they match')
else
  print('they don't match')
end


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 April 2013 - 04:30 AM

View PostSammich Lord, on 22 April 2013 - 04:27 AM, said:

print('they don't match')
BUGFIX:
print("they don't match")


#4 H4X0RZ

  • Members
  • 1,315 posts
  • LocationGermany

Posted 22 April 2013 - 04:30 AM

View PostSammich Lord, on 22 April 2013 - 04:27 AM, said:

if 'stringTest1' == 'stringText1' then
  print('they match')
else
  print('they don't match')
end
Thx :)

#5 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 22 April 2013 - 04:33 AM

View Posttheoriginalbit, on 22 April 2013 - 04:30 AM, said:

View PostSammich Lord, on 22 April 2013 - 04:27 AM, said:

print('they don't match')
BUGFIX:
print("they don't match")
I just came home from my girlfriend's house when it is 49F outside. :P I am could and tired :P I should stop coming on Ask A Pro at 8AM...

#6 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 22 April 2013 - 05:16 AM

View PostSammich Lord, on 22 April 2013 - 04:33 AM, said:

I am could and tired
Nice grammar :P

local function check(firstStr, secStr)
 if firstStr ~= secStr then
  return false
 end
 return true
end
Everything is better with functions ^_^

#7 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 22 April 2013 - 05:17 AM

Since SHA1 is case-insensitive, I'd add to the above:
if string.lower(hash1) == string.lower(hash2) then
  print('they match')
else
  print('they don\'t match')
end

string.lower(), as you might guess, makes every letter lowercase and in the example above I do that with both hashes.
This makes sure that both are lowercase, which essentially means that we eliminated any case-differences.

#8 Sammich Lord

    IRC Addict

  • Members
  • 1,212 posts
  • LocationThe Sammich Kingdom

Posted 22 April 2013 - 05:19 AM

View PostSuicidalSTDz, on 22 April 2013 - 05:16 AM, said:

View PostSammich Lord, on 22 April 2013 - 04:33 AM, said:

I am could and tired
Nice grammar :P
*Cold. That just shows how much I suck at spelling at 8AM :P

#9 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 22 April 2013 - 05:20 AM

View PostSuicidalSTDz, on 22 April 2013 - 05:16 AM, said:

local function check(firstStr, secStr)
if firstStr ~= secStr then
  return false
end
return true
end
Everything is better with functions ^_^
Why not just
local function check(firstStr, secStr)
  return firstStr == secStr
end
One of my pet peeves is when people use an if statement to then only return a boolean, just return the if statements condition!!!

#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 22 April 2013 - 05:32 AM

View Posttheoriginalbit, on 22 April 2013 - 05:20 AM, said:

One of my pet peeves is when people use an if statement to then only return a boolean, just return the if statements condition!!!
Hehe, I'll have to remember that :P (Not to annoy you of course, I would never do that :))

EDIT: The if statement was for the OP, so he wouldn't be confused





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users