Flash / Flex / ActionScript/String/String concatenation

Материал из Web эксперт
Перейти к: навигация, поиск

ActionScript supports the addition (+) operator for text concatenation.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var greeting:String = "Hello, my name is . This is way easier!";
        trace(greeting); 
    }
  }
}



Add strings together

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "A.";
        example += " B ";
        example += "C.";
        
        trace(example);

    }
  }
}



Append text to existing strings by using the concatenation assignment += operator:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var attendance:int = 24;
        var example:String = "There are ";
        example += attendance;
        
        example += " people";
        
        trace(example);
    }
  }
}



Concatenate string by adding integers with string

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var first:int = 24;
        var second:int = 42;
        
        var result:String = "" + first + second;
        trace(result);
    }
  }
}



Joining Strings

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



Joining Strings with "+"

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
       var example:String = "This" + "works";
       var attendance:int = 24;
       var output:String = "There are " + attendance + " people";
       
       trace(output);
    }
  }
}



Use concat method to concatenate two strings

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



Use plus operator when defining the string variable

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "A. "
            + "B"
            + " C.";
    }
  }
}