< >
Home » Arduino内置教程 » Arduino内置教程-字符串-String Comparison Operators

Arduino内置教程-字符串-String Comparison Operators

字符串比较运算符

  • 字符串比较运算符==, !=,>, < ,>=, <=,equals() 和 equalsIgnoreCase()函数允许你在字符之间进行字母比较。这个在分类和排序上很有用处。

  • 运算符 == 和 equals()函数是一样的。换句话,

 if (stringOne.equals(stringTwo)) {

等同于

 if (stringOne ==stringTwo) {
  • ">"(大于)和"<"(小于)运算符根据字母表来在最开始的字符分析字符串。所以,例如,"a" < "b" 和 "1" < "2", 但"999" > "1000" ,因为9跟在1的后面。

  • 注意:当你比较数字字符串时,字符串比较运算符可以会拒绝,因为数字是被看成字符串而不是数字。如果你需要比较数字,把它们作为ints, floats, 或 longs等数据类型比较,而不是作为字符串。

硬件要求

  • Arduino or Genuino开发板

电路

  • 这个例子不需要连接额外的电路,除了你的开发板需要连接到你的电脑,并且打开Arduino IDE的串口监视器窗口。

请输入图片描述
图由 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);

    }
  }
}

[Get Code]

更多

  • String object – 字符串对象的参考
  • CharacterAnalysis - 使用operators来识别对应的特征类型。
  • StringAdditionOperator - 用不同方法把字符串加到一起。
  • StringAppendOperator - 用+=运算符和concat()方法来添加东西到字符串里。
  • StringCaseChanges - 改变字符串的状态。
  • StringCharacters - 在字符串里获得或设置一个指定的字符的值
  • StringComparisonOperators - 按字母排列顺序地比较字符串
  • StringConstructors - 初始化字符串对象
  • StringIndexOf - 寻找在字符串里字符的第一个或最后一个的状态
  • StringLength - 获得和修剪字符串的长度
  • StringLengthTrim - 获得和修剪字符串的长度
  • StringReplace - 替换字符串里的个别字符
  • StringStartsWithEndsWith - 检查一个给定的字符或子串(substrings)的开始或结尾
  • StringSubstring - 在给定的字符串里寻找"phrases"
  • StringToInt - 允许你把字符串转换成整数数字

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: arduino内置教程, arduino string comparison operators