html tool

2015年1月14日星期三

arduino的string比较

def:定义power 开关参数时使用的string,但char比较时发现arduino中字符串比较函数无法使用
参考:http://arduino.cc/en/Tutorial/StringComparisonOperators
解释:
arduino支持string对象,提供比较运算符的重载功能
//equal
  // two strings equal:
  if (stringOne == "this") {
    Serial.println("StringOne == \"this\"");
  }
//也可以用equals描述
if (stringOne.equals(stringTwo)) {

特殊说明:
The greater than and less than operators evaluate strings in alphabetical order, on the first character where the two differ. So, for example "a" < "b" and "1" < "2", but "999"> "1000" because 9 comes after 1.
[popexizhi]
><的重载是使用string的字符串比较,所以999与1000的结果就无可厚非了:).本来想解释但发现只要用string的观点看就没有什么可以解释的了:)
Caution: String comparison operators can be confusing when you're comparing numeric strings, because you're used to thinking of them as numbers, not strings. If you have to compare numbers, compare them as ints, floats, or longs, and not as Strings.
[popexizhi]
这个说明arduino还是很java或c++的类型比较用指定类型不要期望string做什么别的:)

没有评论:

发表评论