Apache flex 有没有办法在Flex中格式化字符串
是否可以这样做:Apache flex 有没有办法在Flex中格式化字符串,apache-flex,string,format,Apache Flex,String,Format,是否可以这样做: var s:String = format("%20d %-10s %s", time, type, message); 在C语言、C++语言、C语言、Python语言和Perl语言中,有一些类似于我的例子,但我似乎找不到Flex。< /P> 我不想为我要格式化的每个字符串创建特殊的类格式化程序。您可能正在寻找mx.utils.StringUtil.substitute()。工作原理与.NET String.Format()类似 例如: StringUtil.substitu
var s:String = format("%20d %-10s %s", time, type, message);
在C语言、C++语言、C语言、Python语言和Perl语言中,有一些类似于我的例子,但我似乎找不到Flex。< /P>
我不想为我要格式化的每个字符串创建特殊的类格式化程序。您可能正在寻找mx.utils.StringUtil.substitute()。工作原理与.NET String.Format()类似 例如:
StringUtil.substitute("Hello {0}", ["World"]);
这个项目听起来应该适合您的需要找到了这个实现sprintf的脚本,这正是我想要的。感谢Manish Jethani
/* sprintf(3) implementation in ActionScript 3.0.
*
* Author: Manish Jethani (manish.jethani@gmail.com)
* Date: April 3, 2006
* Version: 0.1
*
* Copyright (c) 2006 Manish Jethani
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package
{
/* sprintf(3) implementation in ActionScript 3.0.
*
* http://www.die.net/doc/linux/man/man3/sprintf.3.html
*
* The following flags are supported: '#', '0', '-', '+'
*
* Field widths are fully supported. '*' is not supported.
*
* Precision is supported except one difference from the standard: for an
* explicit precision of 0 and a result string of "0", the output is "0"
* instead of an empty string.
*
* Length modifiers are not supported.
*
* The following conversion specifiers are supported: 'd', 'i', 'o', 'u', 'x',
* 'X', 'f', 'F', 'c', 's', '%'
*
* Report bugs to manish.jethani@gmail.com
*/
public function sprintf(format:String, ... args):String
{
var result:String = "";
var length:int = format.length;
for (var i:int = 0; i < length; i++)
{
var c:String = format.charAt(i);
if (c == "%")
{
var pastFieldWidth:Boolean = false;
var pastFlags:Boolean = false;
var flagAlternateForm:Boolean = false;
var flagZeroPad:Boolean = false;
var flagLeftJustify:Boolean = false;
var flagSpace:Boolean = false;
var flagSign:Boolean = false;
var fieldWidth:String = "";
var precision:String = "";
c = format.charAt(++i);
while (c != "d"
&& c != "i"
&& c != "o"
&& c != "u"
&& c != "x"
&& c != "X"
&& c != "f"
&& c != "F"
&& c != "c"
&& c != "s"
&& c != "%")
{
if (!pastFlags)
{
if (!flagAlternateForm && c == "#")
flagAlternateForm = true;
else if (!flagZeroPad && c == "0")
flagZeroPad = true;
else if (!flagLeftJustify && c == "-")
flagLeftJustify = true;
else if (!flagSpace && c == " ")
flagSpace = true;
else if (!flagSign && c == "+")
flagSign = true;
else
pastFlags = true;
}
if (!pastFieldWidth && c == ".")
{
pastFlags = true;
pastFieldWidth = true;
c = format.charAt(++i);
continue;
}
if (pastFlags)
{
if (!pastFieldWidth)
fieldWidth += c;
else
precision += c;
}
c = format.charAt(++i);
}
switch (c)
{
case "d":
case "i":
var next:* = args.shift();
var str:String = String(Math.abs(int(next)));
if (precision != "")
str = leftPad(str, int(precision), "0");
if (int(next) < 0)
str = "-" + str;
else if (flagSign && int(next) >= 0)
str = "+" + str;
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "o":
var next:* = args.shift();
var str:String = uint(next).toString(8);
if (flagAlternateForm && str != "0")
str = "0" + str;
if (precision != "")
str = leftPad(str, int(precision), "0");
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "u":
var next:* = args.shift();
var str:String = uint(next).toString(10);
if (precision != "")
str = leftPad(str, int(precision), "0");
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "X":
var capitalise:Boolean = true;
case "x":
var next:* = args.shift();
var str:String = uint(next).toString(16);
if (precision != "")
str = leftPad(str, int(precision), "0");
var prepend:Boolean = flagAlternateForm && uint(next) != 0;
if (fieldWidth != "" && !flagLeftJustify
&& flagZeroPad && precision == "")
str = leftPad(str, prepend
? int(fieldWidth) - 2 : int(fieldWidth), "0");
if (prepend)
str = "0x" + str;
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
if (capitalise)
str = str.toUpperCase();
result += str;
break;
case "f":
case "F":
var next:* = args.shift();
var str:String = Math.abs(Number(next)).toFixed(
precision != "" ? int(precision) : 6);
if (int(next) < 0)
str = "-" + str;
else if (flagSign && int(next) >= 0)
str = "+" + str;
if (flagAlternateForm && str.indexOf(".") == -1)
str += ".";
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else if (flagZeroPad && precision == "")
str = leftPad(str, int(fieldWidth), "0");
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "c":
var next:* = args.shift();
var str:String = String.fromCharCode(int(next));
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "s":
var next:* = args.shift();
var str:String = String(next);
if (precision != "")
str = str.substring(0, int(precision));
if (fieldWidth != "")
{
if (flagLeftJustify)
str = rightPad(str, int(fieldWidth));
else
str = leftPad(str, int(fieldWidth));
}
result += str;
break;
case "%":
result += "%";
}
}
else
{
result += c;
}
}
return result;
}
}
// Private functions
function leftPad(source:String, targetLength:int, padChar:String = " "):String
{
if (source.length < targetLength)
{
var padding:String = "";
while (padding.length + source.length < targetLength)
padding += padChar;
return padding + source;
}
return source;
}
function rightPad(source:String, targetLength:int, padChar:String = " "):String
{
while (source.length < targetLength)
source += padChar;
return source;
}
/*ActionScript 3.0中的sprintf(3)实现。
*
*作者:Manish Jethani(Manish。jethani@gmail.com)
*日期:2006年4月3日
*版本:0.1
*
*版权所有(c)2006 Manish Jethani
*
*特此免费向获得许可的任何人授予许可
*本软件和相关文档文件的副本(“软件”),
*不受限制地处理软件,包括但不限于
*使用、复制、修改、合并、发布、分发、再许可、,
*和/或出售软件副本,并允许
*为此提供的软件应符合以下条件:
*
*上述版权声明和本许可声明应包含在
*软件的所有副本或主要部分。
*
*本软件按“原样”提供,无任何形式的明示或明示担保
*默示,包括但不限于适销性保证,
*适用于特定目的和非侵权。在任何情况下
*作者或版权持有人应承担任何索赔、损害或其他责任
*无论是合同诉讼、侵权诉讼还是其他诉讼中产生的责任
*来自、不属于或与软件、使用或其他相关
*软件的交易。
*/
包裹
{
/*sprintf(3)在ActionScript3.0中的实现。
*
* http://www.die.net/doc/linux/man/man3/sprintf.3.html
*
*支持以下标志:“#”、“0”、““-”、“+”
*
*完全支持字段宽度。不支持“*”。
*
*支持精度,但与标准有一个区别:对于
*显式精度为0,结果字符串为“0”,输出为“0”
*而不是空字符串。
*
*不支持长度修饰符。
*
*支持以下转换说明符:“d”、“i”、“o”、“u”、“x”,
*‘X’、‘f’、‘f’、‘c’、‘s’、‘%’
*
*向曼尼什报告错误。jethani@gmail.com
*/
公共函数sprintf(格式:String,…args):String
{
var结果:String=“”;
变量长度:int=format.length;
对于(变量i:int=0;i=0)
str=“+”+str;
如果(字段宽度!=“”)
{
如果(flagLeftJustify)
str=右键盘(str,int(fieldWidth));
else if(flagZeroPad&&precision==“”)
str=左键盘(str,int(fieldWidth),“0”);
其他的
str=左键盘(str,int(fieldWidth));
}
结果+=str;
打破
案例“o”:
var next:*=args.shift();
var str:String=uint(next).toString(8);
如果(flagAlternateForm&&str!=“0”)
str=“0”+str;
如果(精度!=“”)
str=左键盘(str,int(精度),“0”);
如果(字段宽度!=“”)
{
如果(flagLeftJustify)
str=右键盘(str,int(fieldWidth));
else if(flagZeroPad&&precision==“”)
str=左键盘(str,int(fieldWidth),“0”);
其他的
str=左键盘(str,int(fieldWidth));
}
结果+=str;
打破
案例“u”:
var next:*=args.shift();
var str:String=uint(next).toString(10);
如果(精度!=“”)
str=左键盘(str,int(精度),“0”);
如果(字段宽度!=“”)
{
如果(flagLeftJustify)
str=右键盘(str,int(fieldWidth));
else if(flagZeroPad&&precision==“”)
str=左键盘(str,int(fieldWidth),“0”);
其他的
str=左键盘(str,int(fieldWidth));
}
结果+=str;
打破
案例“X”:
var Capitalize:布尔值=真;
案例“x”:
var next:*=args.shift();
var str:String=uint(next).toString(16);
如果(精度!=“”)
str=左键盘(str,int(精度),“0”);
变量前置:布尔值=flagAlternateForm&&uint(下一步)!=0;
如果(fieldWidth!=“”&&!flagLeftJustify
&&flagZeroPad&&precision==“”)
str=左键盘(str,前置
?int(fieldWidth)-2:int(fieldWidth),“0”);
如果(准备