String Comparisons in Swift


(John Myers) #1

I've had some difficulty comparing string variables to string constants
with ==. I believe it's always after using a readLine(). Does Swift
require a .equals() or a .compareTo(), as other c like languages, or are
there invisible characters in my string because of the readLine() which I
need to strip out?
Thanks!

···

--
John Myers
------------------------------------------------------------------
myersj@shawanoschools.com


(Austin Zheng) #2

You should be able to use ==.

Are the lengths of your strings the same length? readLine() has a strip newlines parameter that is true by default.

Best,
Austin

···

Sent from my iPhone

On Jun 2, 2016, at 12:07 PM, John Myers via swift-users <swift-users@swift.org> wrote:

I've had some difficulty comparing string variables to string constants with ==. I believe it's always after using a readLine(). Does Swift require a .equals() or a .compareTo(), as other c like languages, or are there invisible characters in my string because of the readLine() which I need to strip out?
Thanks!
--
John Myers
------------------------------------------------------------------
myersj@shawanoschools.com

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users


(Erica Sadun) #3

Have you compared characters.count on the two strings? Could you be pulling in \r\n?

-- E

···

On Jun 2, 2016, at 1:07 PM, John Myers via swift-users <swift-users@swift.org> wrote:

I've had some difficulty comparing string variables to string constants with ==. I believe it's always after using a readLine(). Does Swift require a .equals() or a .compareTo(), as other c like languages, or are there invisible characters in my string because of the readLine() which I need to strip out?
Thanks!