| 前一修订版 |
— | learing:examples:string_comparison_operators [2023/06/07 04:23] (当前版本) – 外部编辑 127.0.0.1 |
---|
| ====== String Comparison Operators (字符比较)====== |
| |
| 字符比较运算符 ==, !=,>, < ,>=, <= 和 equals() equalsIgnoreCase()使你可以在字符串里比较字母。 |
| == 和equals() 是等价的,也就是说 |
| |
| <code cpp>if (stringOne.equals(stringTwo)) {</code> |
| |
| 等同于 |
| |
| <code cpp>if (stringOne ==stringTwo) {</code> |
| |
| ">" (大于) 和 "<" (小于) 运算符以字母顺序比较字符串 , 比较两个不同字符串的第一个字符.所以, 例如 "a" < "b" 和 "1" < "2", 但是 "999" > "1000" 因为9在1后面. |
| |
| 注意,当你比较数字字符串的时候字符串的比较运算可能会出问题,因为数字被处理成字符串了而不是数字。如果你需要比较数字,作为整型或者浮点型比较它们。 |
| |
| ===== 硬件要求 ===== |
| |
| |OCROBOT控制器| |
| |USB线| |
| |
| 这个例子没有电路图,只需要通过USB线把你的OCROBOT控制器连上电脑,并且打开串口监视器。 |
| ===== ALPHA MEGA328-U核心 ===== |
| |
| ==== 硬件 ==== |
| * [[ocrobot:alpha:mega328-u:main|ALPHA MEGA328-U]] |
| |
| ==== 搭建电路 ==== |
| |
| - USB线连接计算机与ALPHA MEGA328-U。 |
| |
| ==== 代码 ==== |
| |
| <code cpp>/* |
| Comparing Strings |
| */ |
| |
| String stringOne, stringTwo; |
| |
| void setup() { |
| // 串口通信 |
| Serial.begin(9600); |
| stringOne = String("this"); |
| stringTwo = String("that"); |
| // 标题 |
| Serial.println("\n\nComparing Strings:"); |
| Serial.println(); |
| |
| } |
| |
| void loop() { |
| // 两个字符串相等: |
| if (stringOne == "this") { |
| Serial.println("StringOne == \"this\""); |
| } |
| // 两个字符串不想等 |
| if (stringOne != stringTwo) { |
| Serial.println(stringOne + " =! " + stringTwo); |
| } |
| |
| // 两个字符串不相等 (注意大小写): |
| stringOne = "This"; |
| stringTwo = "this"; |
| if (stringOne != stringTwo) { |
| Serial.println(stringOne + " =! " + stringTwo); |
| } |
| //你也可以使用equals() 看两个字符串是否相等 |
| if (stringOne.equals(stringTwo)) { |
| Serial.println(stringOne + " equals " + stringTwo); |
| } else { |
| Serial.println(stringOne + " does not equal " + stringTwo); |
| } |
| |
| // 或者你可以忽略大小写 |
| if (stringOne.equalsIgnoreCase(stringTwo)) { |
| Serial.println(stringOne + " equals (ignoring case) " + stringTwo); |
| } else { |
| Serial.println(stringOne + " does not equal (ignoring case) " + stringTwo); |
| } |
| |
| //数字的字符串,比较他们代表的的数字 |
| stringOne = "1"; |
| int numberOne = 1; |
| if (stringOne.toInt() == numberOne) { |
| Serial.println(stringOne + " = " + numberOne); |
| } |
| |
| |
| |
| // 两个数字型的字符串比较 |
| stringOne = "2"; |
| stringTwo = "1"; |
| if (stringOne >= stringTwo) { |
| Serial.println(stringOne + " >= " + stringTwo); |
| } |
| |
| // 比较运算符也可以比较字母型的字符串 |
| 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"); |
| } |
| |
| // compareTo()运算符可以比较字符串,它比较第一个不同的字符,如果比较的字符串的第一个字符按字母排序排在前面,compareTo() 就大于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() 使用起来是很方便的,当你得到得到字符串和字符串里的数字 |
| 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); |
| |
| } |
| } |
| }</code> |