Title here
Summary here
package {
import flash.display.Sprite;
import flash.text.TextField;
public class StringFormatting extends Sprite {
public function StringFormatting() {
var output:TextField = new TextField();
output.width = 400;
output.height = 400;
addChild(output);
function print(str:String):void {
output.appendText(str + "\n");
}
// ActionScript doesn't have a built-in struct type, so we'll use a class
class Point {
public var x:int;
public var y:int;
public function Point(x:int, y:int) {
this.x = x;
this.y = y;
}
}
var p:Point = new Point(1, 2);
// ActionScript doesn't have a printf-like function, so we'll use string concatenation
print("struct1: {" + p.x + " " + p.y + "}");
print("struct2: {x:" + p.x + " y:" + p.y + "}");
// ActionScript doesn't have a direct equivalent to %#v, so we'll approximate it
print("struct3: new Point(" + p.x + ", " + p.y + ")");
// Type information
print("type: " + getQualifiedClassName(p));
// Boolean formatting
print("bool: " + true);
// Integer formatting
print("int: " + 123);
// Binary representation
print("bin: " + 14.toString(2));
// Character from integer
print("char: " + String.fromCharCode(33));
// Hexadecimal
print("hex: " + 456.toString(16));
// Float formatting
print("float1: " + 78.9);
// Scientific notation
print("float2: " + 123400000.0.toExponential(6));
print("float3: " + 123400000.0.toExponential(6).toUpperCase());
// String formatting
print("str1: \"string\"");
print("str2: \"\\\"string\\\"\"");
// Hex representation of a string
var hexStr:String = "";
for (var i:int = 0; i < "hex this".length; i++) {
hexStr += "hex this".charCodeAt(i).toString(16);
}
print("str3: " + hexStr);
// Pointer-like representation (ActionScript doesn't have pointers)
print("pointer: " + p);
// Width formatting for numbers (ActionScript doesn't have built-in padding)
function padLeft(str:String, width:int):String {
while (str.length < width) {
str = " " + str;
}
return str;
}
print("width1: |" + padLeft("12", 6) + "|" + padLeft("345", 6) + "|");
// Width formatting for floats
function padFloat(num:Number, width:int, precision:int):String {
var str:String = num.toFixed(precision);
return padLeft(str, width);
}
print("width2: |" + padFloat(1.2, 6, 2) + "|" + padFloat(3.45, 6, 2) + "|");
// Left-justified width formatting
function padRight(str:String, width:int):String {
while (str.length < width) {
str += " ";
}
return str;
}
print("width3: |" + padRight("1.20", 6) + "|" + padRight("3.45", 6) + "|");
// Width formatting for strings
print("width4: |" + padLeft("foo", 6) + "|" + padLeft("b", 6) + "|");
print("width5: |" + padRight("foo", 6) + "|" + padRight("b", 6) + "|");
// Sprintf-like functionality (ActionScript doesn't have this built-in)
var s:String = "sprintf: a " + "string";
print(s);
// Writing to different outputs (ActionScript doesn't have multiple standard streams)
print("io: an error");
}
}
}
This ActionScript code demonstrates various string formatting techniques, attempting to replicate the functionality shown in the original example. However, it’s important to note that ActionScript doesn’t have built-in formatting functions like printf
, so we’ve had to implement some of this functionality manually.
Key differences and adaptations:
TextField
to display output instead of console logging.stdout
and stderr
, so all output goes to the same place.To run this code, you would need to set up a Flash development environment and create a new ActionScript project with this as the main class.