0°

Arduino内置教程-字符串-字符串索引Index Of

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

简介

  • 字符串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的串口监视器窗口。

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

样例代码

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);
}
「点点赞赏,手留余香」

    还没有人赞赏,快来当第一个赞赏的人吧!
0 条回复 A 作者 M 管理员
    所有的伟大,都源于一个勇敢的开始!
欢迎您,新朋友,感谢参与互动!欢迎您 {{author}},您在本站有{{commentsCount}}条评论