How can I compare two strings to see if one is greater than the other

how to compare two strings in java without using equals
how to compare two strings in java using if
how to compare two strings in java which is greater
how to compare two strings in javascript if condition
compare strings java
how to compare characters in a string in java
string comparison online
string compare

I am working on a basic card game. I have the user shuffle two cards and I want to be able to know if the cards are equal or if one is greater than the other or less than the other. Currently I can only tell if they are equal.

   //Randomize Dealer Card
func randomDealerNumber(){

    let randomNumber = arc4random_uniform(8)
    if randomNumber == 0 {

        dealerNum.text = "10"
    } else if randomNumber == 1 {

        dealerNum.text = "1"
    } else if randomNumber == 2 {

        dealerNum.text = "2"
    } else if randomNumber == 3 {

        dealerNum.text = "3"
    } else if randomNumber == 4 {

        dealerNum.text = "4"
    }else if randomNumber == 5 {

        dealerNum.text = "5"
    } else if randomNumber == 6 {

        dealerNum.text = "6"
    } else if randomNumber == 7 {

        dealerNum.text = "7"

let playerCard = playerNum.text
let dealerCard = dealerNum.text
if playerCard == dealerCard {
    print("These two strings are considered equal")

You shouldn't store values in your UIControls. The values should be in variables that represent your model and then just compare the values in your model.

I would suggest keeping the random values you generate as Int and then just compare them. Adding property observers (didSet) to those will allow you to set the labels when those value are set:

class ViewController: UIViewController {
    @IBOutlet weak var dealerNum: UILabel!
    @IBOutlet weak var playerNum: UILabel!

    var dealerCard = 0 {
        didSet {
            dealerNum.text = String(dealerCard)

    var playerCard = 0 {
        didSet {
            playerNum.text = String(playerCard)

    func randomDealerNumber() {
        var randomNumber = Int.random(in: 0...7)
        if randomNumber == 0 {
            randomNumber = 10
        dealerCard = randomNumber

    func compare() {
        if playerCard == dealerCard {
        } else if playerCard < dealerCard {
            print("less than")
        } else {
            print("greater than")

How can I compare two strings to see if one is greater than the other , Java String compareTo() method is used to compare two strings, this method is case The result is positive if the first string is lexicographically greater than the second Here we have three Strings and we are comparing them with each other using Here we will see an interesting example of how to use the compareTo()  By calling the ReferenceEquals method, you can see that the two strings actually refer to the same object in memory. Use the String.Copy method to avoid interning. After the copy has been made, the two strings have different storage locations, even though they have the same value.

Maybe there is some oversight here, but if I am understanding correctly then your solution is quite simple. Another if statement:

if playerCard > dealerCard{
    print("Player wins!")

PPShein is correct. If you are talking about strings you should be using count.

Java String compareTo() Method with examples, When one uses == operator for s1 and s2 comparison then the result is false as both have different addresses in memory. Using equals, the result is true because  Use '==' to compare two string objects. 5.10.3. Compare for equal. 5.10.4. Compare string: equal, less than or greater than. 5.10.5. If two string are equal. 5.10.6. Compare string case sensitively.

Basically you want to compare the integer values of the text fields. So you need to convert them to integer. (e.g. "10" to 10)

You can do like this

if(Int(playerCard) > Int(dealerCard)){......}

Just make sure you add sufficient safety checks to verify if the entered value can be converted to integer or not.

Compare two Strings in Java, Given two strings, how to check if the two strings are equal or not. Examples: Input : ABCD, XYZ Output : ABCD is not equal to XYZ XYZ is greater than ABCD to a certain operation, while compare() can perform lots of different operations See your article appearing on the GeeksforGeeks main page and help other Geeks. The method compareTo () is used for comparing two strings lexicographically in Java. Each character of both the strings is converted into a Unicode value for comparison. int compareTo (String str) : It returns the following values: if (string1 > string2) it returns a positive value. if both the strings are equal lexicographically.

Comparing two strings in C++, When I say comparing String variables, it can be either to compare two String and then compare using == operator, you will get the right answer, but if you take but cannot be used to check if one string is greater than or less than other. String.Compare method compares two strings and returns an integer value. The return value of the Compare method can be less than zero, greater than zero or equals to zero. The code sample in Listing 2 is an example of comparing two strings using String.Compare method. string author1 = "Mahesh Chand";

Right way to Compare String in Java, Perl has different operators (relational and equality operators)for comparing listed above are used to test the relationship between two operands. You can see if one operand is equal to another, if one operand is greater than another, or if  We can compare any substring at any position in a given string using compare(), which otherwise requires the long procedure of word by word extraction of string for comparison using relational operators.

Comparison operators for numbers and strings, equals() method compare two strings for value equality, whether they are logically equal. equals() method in String class takes another string as a parameter and In Java Strings, the == operator is used to check the reference of both lexicographically greater than the specified string, i.e. if the argument  Using String.equalsIgnoreCase() : The String.equalsIgnoreCase() method compares two strings irrespective of the case (lower or upper) of the string. This method returns true if the argument is not null and the contents of both the Strings are same ignoring case, else false.