Flash / Flex / ActionScript/String/indexOf lastIndexOf

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Find all the occurrences of a substring.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript Bible");
        var nMatch:Number = sTitle.indexOf("i");
        while(nMatch != -1) {
          trace(nMatch);
          nMatch = sTitle.indexOf("i", nMatch + 1);
        }
         

    }
  }
}
3
9
14



Finding Substrings

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript");
        trace(sTitle.indexOf("ActionScript")); 
        trace(sTitle.indexOf("i"));  
        trace(sTitle.lastIndexOf("i"));  
        trace(sTitle.indexOf("i", 4));  
        trace(sTitle.lastIndexOf("i", 12));  
        trace(sTitle.indexOf("q"));  
        trace(sTitle.lastIndexOf("g"));  
        
    }
  }
}
0
3
9
9
9
-1
-1



Is indexOf case sensitive

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "Cool. This is a cool as both cool (lowercase) and Cool.";
        
        var search:String = "cool";
        
        trace( example.indexOf( search ) );

    }
  }
}



Returns everything before the last period, if any.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace( removeExtension( "document.jpg" ) );       // Displays: document
        trace( removeExtension( "document" ) );           // Displays: document
        trace( removeExtension( "document.1.jpg" ) );     // Displays: document.1
        trace( extractExtension( "document.jpg" ) );      // Displays: .jpg
        trace( extractExtension( "document" ) );          // Displays nothing
        trace( extractExtension( "document.1.jpg" ) );    // Displays: .jpg

    }
    private function removeExtension( filename:String ):String {
      var extensionIndex:Number = filename.lastIndexOf( "." );
      if ( extensionIndex == -1 ) {
        return filename;
      } else {
        return filename.substr( 0, extensionIndex );
      } 
    }
    
    private function extractExtension( filename:String ):String {
      var extensionIndex:Number = filename.lastIndexOf( "." );
      if ( extensionIndex == -1 ) {
        return "";
      } else {
        return filename.substr( extensionIndex + 1, filename.length );
      } 
    }
  }
}



Searching for a Substring

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "This string contains the word cool twice. Very cool.";
        var index:int = example.indexOf( "cool" );
        if ( index != -1 ) {
          trace( "String contains word cool at index " + index );
        }
    }
  }
}



Use indexOf and lastIndexOf to locate specific string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sEmail:String = new String("info@your.ru");
         
        var nCheckOne:Number = sEmail.indexOf("@");
        var nCheckTwo:Number = sEmail.lastIndexOf(".");
        if (nCheckOne!= -1 && nCheckTwo!= -1 && nCheckOne < nCheckTwo){
            trace("good email address");
        }
    }
  }
}



Use indexOf( ) in a while statement to get the indices of every occurrence of a substring

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "This string contains the word cool. Very cool. Yes, cool.";
        var index:int =  -1;
        while ( ( index = example.indexOf( "cool", index + 1 ) ) != -1 ) {
        
          trace( "String contains word cool at index " + index );
        }
    }
  }
}



Use indexOf in while loop to count the instance of a substring

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var story:String = "It was a dark and stormy night...";
        var pattern:String = "a";
        var count:int = 0;
        var startIndex:int = 0;
        while (story.indexOf(pattern, startIndex) != -1) {
           count++;
           startIndex = story.indexOf(pattern, startIndex) + 1;
        }
        trace(count);
    }
  }
}



Use indexOf to check the existance of an substring

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript Demo");
        var nIndex:Number = sTitle.indexOf("Script");
        if(nIndex != -1) {
          trace("substring was found");
        }
    }
  }
}



Use lastIndexOf to search string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "This string contains the word cool twice. Very cool.";
        
        var index:int =  example.lastIndexOf( "cool" );
        
        if ( index != -1 ) {
          trace( "String contains word cool at index " + index );
        }
        index = example.lastIndexOf( "cool", index - 1 );
        
        if ( index != -1 ) {
          trace( "String contains word cool at index " + index );
        }
    }
  }
}



Use offset index in indexOf method to search substring

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "This string contains the word cool twice. Very cool.";
        var index:int = example.indexOf( "cool" );
        
        if ( index != -1 ) {
          trace( "String contains word cool at index " + index );
        }
        
        index = example.indexOf( "cool", index + 1 );
        
        if ( index != -1 ) {
          trace( "String contains word cool at index " + index );
        }
    }
  }
}



Use offset in lastIndexOf method

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "This string contains the word cool. Very cool. Yes, cool.";
        
        var index:int =  example.length;
        
        while ( ( index = example.lastIndexOf( "cool", index - 1 ) ) != -1 ) {
          trace( "String contains word cool at index " + index );
        }
    }
  }
}



Use the lastIndexOf method to get the file extension name

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var filename:String = "document.jpg";
        var extensionIndex:Number = filename.lastIndexOf( "." );
        
        var extension:String = filename.substr( extensionIndex + 1, filename.length );
        trace( "The file extension is " + extension );
    }
  }
}