Τόσο οι τελεστές σύγκρισης συμβολοσειράς ==, !=, >, < , >=, <=, όσο και οι μέθοδοι equals() και equalsIgnoreCase(), σας επιτρέπουν να κάνετε αλφαβητικές συγκρίσεις μεταξύ συμβολοσειρών. Μεταξύ των άλλων, αυτοί είναι χρήσιμοι για την διαλογή και την αλφαβητική ταξινόμηση.

Ο τελεστής == καθώς και η μέθοδος equals() εκτελούν την ίδια ακριβώς εργασία. Με άλλα λόγια, η παρακάτω εντολή:

if (stringOne.equals(stringTwo)) {

Είναι ακριβώς ίδια με την ακόλουθη εντολή:

if (stringOne ==stringTwo) {

Οι τελεστές “>” (μεγαλύτερο από) και “<” (μικρότερο από), ταξινομούν τις συμβολοσειρές σε αλφαβητική σειρά. Συγκεκριμένα, αυτό υλοποιείται στον πρώτο χαρακτήρα, ενώ διαφέρει όσον αφορά τον δεύτερο χαρακτήρα. Έτσι, για παράδειγμα “a” < “b” και “1” < “2”. Όμως “999” > “1000”. Αυτό συμβαίνει διότι το 9 ακολουθεί μετά από το 1.

Προσοχή: Οι τελεστές σύγκρισης συμβολοσειράς, μπορούν να αποδειχθούν εξαιρετικά πολύπλοκοι όταν έχετε να συγκρίνετε αριθμητικές συμβολοσειρές. Αυτό συμβαίνει διότι οι αριθμοί αντιμετωπίζονται ως αλφαριθμητικά και όχι ως αυτούσιοι αριθμοί. Έτσι, αν χρειαστεί να συγκρίνετε αριθμούς, τότε είναι καλύτερα να τους συγκρίνετε ως ακέραιους, δεκαδικούς ή long και όχι ως συμβολοσειρές.

Απαιτούμενος εξοπλισμός

– Πλακέτα του Arduino ή του Genuino.

Κύκλωμα

Δεν υπάρχει κάποιο συγκεκριμένο κύκλωμα για αυτό το παράδειγμα. Ωστόσο, η πλακέτα σας θα πρέπει να είναι συνδεδεμένη με τον υπολογιστή σας μέσω της USB θύρας. Επίσης, θα πρέπει να είναι ενεργοποιημένο και το παράθυρο της σειριακής οθόνης του λογισμικού του Arduino (IDE).

Arduino Board
Η εικόνα δημιουργήθηκε από το πρόγραμμα Fritzing.

Κώδικας

/*
  Comparing Strings

 Examples of how to compare strings using the comparison operators

 created 27 July 2010
 modified 2 Apr 2012
 by Tom Igoe

 http://www.arduino.cc/en/Tutorial/StringComparisonOperators

 This example code is in the public domain.
 */

String stringOne, stringTwo;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  stringOne = String("this");
  stringTwo = String("that");
  // send an intro:
  Serial.println("\n\nComparing Strings:");
  Serial.println();

}

void loop() {
  // two strings equal:
  if (stringOne == "this") {
    Serial.println("StringOne == \"this\"");
  }
  // two strings not equal:
  if (stringOne != stringTwo) {
    Serial.println(stringOne + " =! " + stringTwo);
  }

  // two strings not equal (case sensitivity matters):
  stringOne = "This";
  stringTwo = "this";
  if (stringOne != stringTwo) {
    Serial.println(stringOne + " =! " + stringTwo);
  }
  // you can also use equals() to see if two strings are the same:
  if (stringOne.equals(stringTwo)) {
    Serial.println(stringOne + " equals " + stringTwo);
  } else {
    Serial.println(stringOne + " does not equal " + stringTwo);
  }

  // or perhaps you want to ignore case:
  if (stringOne.equalsIgnoreCase(stringTwo)) {
    Serial.println(stringOne + " equals (ignoring case) " + stringTwo);
  } else {
    Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo);
  }

  // a numeric string compared to the number it represents:
  stringOne = "1";
  int numberOne = 1;
  if (stringOne.toInt() == numberOne) {
    Serial.println(stringOne + " = " + numberOne);
  }



  // two numeric strings compared:
  stringOne = "2";
  stringTwo = "1";
  if (stringOne >= stringTwo) {
    Serial.println(stringOne + " >= " + stringTwo);
  }

  // comparison operators can be used to compare strings for alphabetic sorting too:
  stringOne = String("Brown");
  if (stringOne < "Charles") {
    Serial.println(stringOne + " < Charles");
  }

  if (stringOne > "Adams") {
    Serial.println(stringOne + " > Adams");
  }

  if (stringOne <= "Browne") {
    Serial.println(stringOne + " <= Browne");
  }


  if (stringOne >= "Brow") {
    Serial.println(stringOne + " >= Brow");
  }

  // the compareTo() operator also allows you to compare strings
  // it evaluates on the first character that's different.
  // if the first character of the string you're comparing to
  // comes first in alphanumeric order, then compareTo() is greater than 0:
  stringOne = "Cucumber";
  stringTwo = "Cucuracha";
  if (stringOne.compareTo(stringTwo) < 0) {
    Serial.println(stringOne + " comes before " + stringTwo);
  } else {
    Serial.println(stringOne + " comes after " + stringTwo);
  }

  delay(10000);  // because the next part is a loop:

  // compareTo() is handy when you've got strings with numbers in them too:

  while (true) {
    stringOne = "Sensor: ";
    stringTwo = "Sensor: ";

    stringOne += analogRead(A0);
    stringTwo += analogRead(A5);

    if (stringOne.compareTo(stringTwo) < 0) {
      Serial.println(stringOne + " comes before " + stringTwo);
    } else {
      Serial.println(stringOne + " comes after " + stringTwo);

    }
  }
}

Επιστροφή στην σελίδα Παραπομπές Γλώσσας