Flash / Flex / ActionScript/String/Escape

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

A complete listing of all the escape sequences available to you

 
Escape Sequence      Resulting String 
\b      Backspace character.
\f      Form feed character. This character advances one page and is rarely used.
\n      Newline character. Also known as line feed.
\r      Carriage return character.
\t      Tab character.
\unnnn      Inserts a character with the four-digit hexadecimal Unicode code.
\xnn      Inserts a character with the two-digit hexadecimal ASCII code.
\"      Single quote (") character.
\"      Double quote (") character.
\\      Backslash (\) character.



An example of the newline character used in a string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace("line one\nline two");
    }
  }
}



Display \n literally, instead of a newline, you can add an additional backslash just before it

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace("\\n");  // Displays: \\n
    }
  }
}



Escaping Characters

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var sPhrase:String = "Ain"t ain"t a word."; // correct
        
        var sQuote:String = ""hello"";
    }
  }
}



Inserting Special Whitespace Characters: Backspace \b

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "these\bwords\bare\nseparated\nby\nnewlines";
        
        trace(example);
    }
  }
}



Inserting Special Whitespace Characters: Carriage return \r

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "these\rwords\rare\rseparated\nby\nnewlines";
        
        trace(example);
    }
  }
}



Inserting Special Whitespace Characters: Form feed \f

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "these\fwords\fare\fseparated\nby\nnewlines";
        trace(example);
    }
  }
}



Inserting Special Whitespace Characters: Newline \n

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "these\nwords\nare\nseparated\nby\nnewlines";
        
        trace(example);
    }
  }
}



Inserting Special Whitespace Characters: Tab \t

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:String = "these\twords\tare\tseparated\tby\ttabs";
        trace(example);
    }
  }
}



Passing String Values to and from Applications (escape)

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



The backslash character has a special function when used in a string literal

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        trace("\\");  // displays: \
    }
  }
}



unescape() function does not modify the existing string, but returns a new string

 

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



Using Quotes and Apostrophes in Strings

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var exampleA:String = "He said, "Yes."";
        
        var exampleB:String = "He said, "Yes."";
        
        var exampleC:String = "He said, \"Yes.\"";
        
        trace(exampleA);
        trace(exampleB);
        trace(exampleC);
    }
  }
}