< >
Home » Arduino内置教程 » Arduino内置教程-字符串-String Index Of

Arduino内置教程-字符串-String Index Of

字符串indexOf() 和 lastIndexOf() 函数

  • 字符串对象indexOf()函数允许你搜索字符串里的一个特殊字符的第一个例子。你也可以寻找一个补偿后的字符的第一个例子。lastIndexOf()函数使你从字符串后面开始做同样的事。

String stringOne = "";
int firstClosingBracket = stringOne.indexOf('>');

  • 在这种情况下,firstClosingBracket等于5,因为第一个“>”字符在字符串的第5位(第一个字符当0数)。如果你想得到第二个最近的同类项,你可以用你知道第一个同类项的位置的事实,然后从firstClosingBracket + 1的位置开始查找,就像这样:
 stringOne = "<HTML><HEAD><BODY>";
 int secondClosingBracket = stringOne.indexOf('>', firstClosingBracket + 1 );
  • 结果是11,位置在跟在HEAD后面。

  • 如果你想从字符串的后面开始搜索,你可以用 lastIndexOf() 函数来代替。这个函数返回一个给定字符最后出现的位置。

 stringOne = "<HTML><HEAD><BODY>";
 int lastOpeningBracket = stringOne.lastIndexOf('<');
  • 这种情况下,lastOpeningBracket等于12,“<”的位置在BODY后面。如果你想在HEAD后面第一次出现的同类项,可以用stringOne.lastIndexOf('<', lastOpeningBracket -1), 或者是第 6 个。

硬件要求

  • Arduino or Genuino 开发板

电路

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

请输入图片描述
图由 Fritzing 软件绘制

样例代码

/*
  String indexOf() and lastIndexOf() functions

 Examples of how to evaluate, look for, and replace characters in a String

 created 27 July 2010
 modified 2 Apr 2012
 by Tom Igoe

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

 This example code is in the public domain.
 */

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
  }

  // send an intro:
  Serial.println("\n\nString indexOf() and lastIndexOf()  functions:");
  Serial.println();
}

void loop() {
  // indexOf() returns the position (i.e. index) of a particular character
  // in a string. For example, if you were parsing HTML tags, you could use it:
  String stringOne = "<HTML><HEAD><BODY>";
  int firstClosingBracket = stringOne.indexOf('>');
  Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);

  stringOne = "<HTML><HEAD><BODY>";
  int secondOpeningBracket = firstClosingBracket + 1;
  int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket);
  Serial.println("The index of  the second > in the string " + stringOne + " is " + secondClosingBracket);

  // you can also use indexOf() to search for Strings:
  stringOne = "<HTML><HEAD><BODY>";
  int bodyTag = stringOne.indexOf("<BODY>");
  Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);

  stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
  int firstListItem = stringOne.indexOf("<LI>");
  int secondListItem = stringOne.indexOf("<LI>", firstListItem + 1);
  Serial.println("The index of the second list tag in the string " + stringOne + " is " + secondListItem);

  // lastIndexOf() gives you the last occurrence of a character or string:
  int lastOpeningBracket = stringOne.lastIndexOf('<');
  Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);

  int lastListItem  = stringOne.lastIndexOf("<LI>");
  Serial.println("The index of the last list tag in the string " + stringOne + " is " + lastListItem);


  // lastIndexOf() can also search for a string:
  stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
  int lastParagraph = stringOne.lastIndexOf("<p");
  int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
  Serial.println("The index of the second to last paragraph tag " + stringOne + " is " + secondLastGraf);

  // do nothing while true:
  while (true);
}

[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 index of