28章:Arduino(アルドゥイーノ)演習(Strings/StringIndexOf編)

    作成2015.08.28

  1. Arduino(アルドゥイーノ)演習参照アドレス
     Arduino(アルドゥイーノ)演習は下記のアドレスを参照します。
    https://www.arduino.cc/en/Tutorial/HomePage


  2. Strings/StringIndexOfの回路図
     パソコンとのUSB接続のみとなります。


  3. Strings/StringIndexOfのスケッチ
    (1)メニューの「ファイル」_「スケッチの例」_「Strings」_「StringIndexOf」 で以下のスケッチが設定されます。
    /*
      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 Leonardo 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("item", firstListItem + 1 );
      Serial.println("The index of the second list item in the string " + stringOne + " is " + secondClosingBracket);
    
      // 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 item 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 last paragraph tag " + stringOne + " is " + secondLastGraf);
    
      // do nothing while true:
      while (true);
    }
    


  4. Strings/StringIndexOfの実行
    (1)メニューの「スケッチ」_「マイコンボードに書き込む」で書込みされ、実行されます。
    (2)メニューの「ツール」_「シリアルモニタ」を選択するとシリアルモニタが表示されます。
    (3)以下が表示されます。
    String indexOf() and lastIndexOf()  functions:
    
    The index of > in the string <HTML><HEAD><BODY> is 5
    The index of  the second > in the string <HTML><HEAD><BODY> is 11
    The index of the body tag in the string <HTML><HEAD><BODY> is 12
    The index of the second list item in the string <UL><LI>item<LI>item<LI>item</UL> is 11
    The index of the last < in the string <UL><LI>item<LI>item<LI>item</UL> is 28
    The index of the last list item in the string <UL><LI>item<LI>item<LI>item</UL> is 20
    The index of the second last paragraph tag <p>Lorem ipsum dolor sit amet</p><p>Ipsem</p><p>Quod</p> is 33
    


  5. Strings/StringIndexOfまとめ
    (1)文字の位置判定の演習です。




29章:Arduino(アルドゥイーノ)演習(Strings/StringLengthTrim編)に行く。

トップページに戻る。