Flash / Flex / ActionScript/String/String

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

Access string method directly from the String value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace("banana".length); // 6
    }
  }
}



Call the toString() method. This method returns the string representation of any type of object if available.

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

        var sTitle:String = new String("ActionScript Bible");
        
        trace(sTitle.toString()); // Displays: ActionScript Bible
        
        //By default, the toString() method of an object is called if you attempt to use it where Flash is expecting a string. 
        
        var sTitle:String = new String("ActionScript Bible");
        
        trace(sTitle); // Displays: ActionScript Bible
        
    }
  }
}



Call the valueOf() method. The valueOf() method of any class returns the primitive value if one exists:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("ActionScript Bible");
        
        trace(sTitle.valueOf()); // Displays: ActionScript Bible
    }
  }
}



Converting a String Object to a Primitive

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
          var s: String = "Hello, world!";
          trace(s); // Displays: Hello, world!
          trace (s .valueOf ()); // Displays: Hello, world!
    }
  }
}



Convert string to characters

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "a string";
        var characters:Array = example.split( "" );
        characters.sort(  );
        
        for ( var i:int = 0; i < characters.length; i++) {
          trace( characters[i] );
        }
    }
  }
}



Create a String object by simply invoking a property or method of the String class from a primitive string value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var dToday:Date = new Date();
        var sDate:String = new String(dToday.toString());
        trace("ActionScript Bible".length);  // Displays 18
    }
  }
}



Define string variable

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

      var exampleA:String = "this is a string";
      var exampleB:String = "this is also a string";
      var exampleD:String = ""; // Empty string
      var exampleE:String = "x"; // Single character
      var exampleF:String; // Defaults to null when no value is assigned
    }
  }
}



Newline character, \n, which is the equivalent of pressing the Enter key.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var s: String = "Dear Mom, \nThings are swell. I miss you.\nLove, \nMims";
        trace(s);
    }
  }
}



Pass string variables to a method

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       updateChatHistory("Hi", "Joe");
    }
    private function updateChatHistory( message:String, username:String ):void {
      _history += username + ":" + message + "\n";
    };
  }
}



Remove every instance of a particular character

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "a string";
        var characters:Array = example.split( "" );
        
        for ( var i:Number = 0; i < characters.length; i++ ) {
          if ( characters[i] == "r") {
            characters.splice( i, 1 );
            i--;
          }
        }
        trace( characters.join( "" ) );
    }
  }
}



String.concat( ) appends new values to the end of an existing string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var original:String = "original string value.";
        var modified:String = original.concat( "now modified." );
        trace(modified);     
    }
  }
}



The escape characters for double and single quotes are \" and \" respectively.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
          var s: String = "Bugs says \"What\"s up, doc?\"";
          trace(s); // Bugs says "What"s up, doc?"
    }
  }
}



Use valueOf method to return the value of a string object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sTitle:String = new String("wbex.ru");
        trace("the" + sTitle.valueOf() + " rocks!");
    }
  }
}



Using toString to convert object to String

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var now: Date = new Date();
        trace("Today"s date is "+ now.toString ());
    }
  }
}