~ubuntu-branches/debian/experimental/arduino/experimental

« back to all changes in this revision

Viewing changes to build/shared/examples/8.Strings/StringIndexOf/StringIndexOf.ino

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2012-03-11 18:19:42 UTC
  • mfrom: (1.1.5) (5.1.14 sid)
  • Revision ID: package-import@ubuntu.com-20120311181942-be2clnbz1gcehixb
Tags: 1:1.0.1~rc1+dfsg-1
New upstream release, experimental.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  String indexOf() and lastIndexOf() functions
 
3
 
 
4
 Examples of how to evaluate, look for, and replace characters in a String
 
5
 
 
6
 created 27 July 2010
 
7
 by Tom Igoe
 
8
 
 
9
 http://arduino.cc/en/Tutorial/StringIndexOf
 
10
 
 
11
 This example code is in the public domain. 
 
12
 */
 
13
 
 
14
void setup() {
 
15
  Serial.begin(9600);
 
16
  Serial.println("\n\nString indexOf() and lastIndexOf()  functions:");
 
17
 
 
18
}
 
19
 
 
20
void loop() {
 
21
 // indexOf() returns the position (i.e. index) of a particular character
 
22
  // in a string. For example, if you were parsing HTML tags, you could use it:
 
23
  String stringOne = "<HTML><HEAD><BODY>";
 
24
  int firstClosingBracket = stringOne.indexOf('>');
 
25
  Serial.println("The index of > in the string " + stringOne + " is " + firstClosingBracket);
 
26
 
 
27
  stringOne = "<HTML><HEAD><BODY>";
 
28
  int secondOpeningBracket = firstClosingBracket + 1;
 
29
  int secondClosingBracket = stringOne.indexOf('>', secondOpeningBracket );
 
30
  Serial.println("The index of  the second > in the string " + stringOne + " is " + secondClosingBracket);
 
31
 
 
32
  // you can also use indexOf() to search for Strings:
 
33
  stringOne = "<HTML><HEAD><BODY>";
 
34
  int bodyTag = stringOne.indexOf("<BODY>");
 
35
  Serial.println("The index of the body tag in the string " + stringOne + " is " + bodyTag);
 
36
 
 
37
  stringOne = "<UL><LI>item<LI>item<LI>item</UL>";
 
38
  int firstListItem = stringOne.indexOf("<LI>");
 
39
  int secondListItem = stringOne.indexOf("item", firstListItem + 1 );
 
40
  Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);
 
41
 
 
42
  // lastIndexOf() gives you the last occurrence of a character or string:
 
43
  int lastOpeningBracket = stringOne.lastIndexOf('<');
 
44
  Serial.println("The index of the last < in the string " + stringOne + " is " + lastOpeningBracket);
 
45
 
 
46
  int lastListItem  = stringOne.lastIndexOf("<LI>");
 
47
  Serial.println("The index of the last list item in the string " + stringOne + " is " + lastListItem);
 
48
 
 
49
 
 
50
// lastIndexOf() can also search for a string:
 
51
   stringOne = "<p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p>";
 
52
  int lastParagraph = stringOne.lastIndexOf("<p");
 
53
  int secondLastGraf = stringOne.lastIndexOf("<p", lastParagraph - 1);
 
54
  Serial.println("The index of the second last paragraph tag " + stringOne + " is " + secondLastGraf);
 
55
 
 
56
// do nothing while true:
 
57
 while(true);
 
58
}
 
 
b'\\ No newline at end of file'