插入逗号到数字字符串 [英] Insert commas into number string
问题描述
嘿,我想在一个字符串进行向后定期EX pression搜索,将其划分成3位数的组。据我从AS3 <一见href="http://livedocs.adobe.com/flex/3/html/help.html?content=12%5FUsing%5FRegular%5FEx$p$pssions%5F01.html">documentation,反向搜索是不可能的了reg前引擎。
本练习的要点是插入三重逗号为若干个像这样:
千万=&GT; 10,000,000
我想这样做,像这样的:
与string.replace(/(\ d {3})/克,$ 1)
但是,这是不正确的,由于搜索没有从后面发生的事情和替换$ 1将只对第一场比赛。
我越来越觉得我会过得更好使用循环执行此任务。
更新:
由于AS3不支持预测先行这是我如何解决它。
公共静态函数formatNumber(号码:号码):字符串
{
VAR numString:字符串= number.toString()
VAR的结果:string =''
而(numString.length→3)
{
VAR块:字符串= numString.substr(-3)
numString = numString.substr(0,numString.length - 3)
结果=','+大块+结果
}
如果(numString.length大于0)
{
结果= numString +结果
}
返回结果
}
如果你的语言支持阳性向前断言,那么我想下面的正则表达式将工作:
(\ d)(?=(\ d {3})+ $)
展示在Java中:
进口静电org.junit.Assert.assertEquals;
进口org.junit.Test;
公共类CommifyTest {
@测试
公共无效testCommify(){
串NUM0 =1;
串NUM1 =123456;
字符串NUM2 =1234567;
串NUM3 =12345678;
字符串num4 =123456789;
字符串的正则表达式=(\\ D)(=(\\Ð{3})+ $?);
的assertEquals(1,num0.replaceAll(正则表达式,$ 1,));
的assertEquals(123456,num1.replaceAll(正则表达式,$ 1));
的assertEquals(1,234,567,num2.replaceAll(正则表达式,$ 1));
的assertEquals(12345678,num3.replaceAll(正则表达式,$ 1));
的assertEquals(123,456,789,num4.replaceAll(正则表达式,$ 1));
}
}
下面的<一个href="http://livedocs.adobe.com/flex/3/html/help.html?content=12%5FUsing%5FRegular%5FEx$p$pssions%5F09.html#123006">link建议AS3呢?
Hey there, I'm trying to perform a backwards regular expression search on a string to divide it into groups of 3 digits. As far as i can see from the AS3 documentation, searching backwards is not possible in the reg ex engine.
The point of this exercise is to insert triplet commas into a number like so:
10000000 => 10,000,000
I'm thinking of doing it like so:
string.replace(/(\d{3})/g, ",$1")
But this is not correct due to the search not happening from the back and the replace $1 will only work for the first match.
I'm getting the feeling I would be better off performing this task using a loop.
UPDATE:
Due to AS3 not supporting lookahead this is how I have solved it.
public static function formatNumber(number:Number):String
{
var numString:String = number.toString()
var result:String = ''
while (numString.length > 3)
{
var chunk:String = numString.substr(-3)
numString = numString.substr(0, numString.length - 3)
result = ',' + chunk + result
}
if (numString.length > 0)
{
result = numString + result
}
return result
}
If your language supports postive lookahead assertions, then I think the following regex will work:
(\d)(?=(\d{3})+$)
Demonstrated in Java:
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CommifyTest {
@Test
public void testCommify() {
String num0 = "1";
String num1 = "123456";
String num2 = "1234567";
String num3 = "12345678";
String num4 = "123456789";
String regex = "(\\d)(?=(\\d{3})+$)";
assertEquals("1", num0.replaceAll(regex, "$1,"));
assertEquals("123,456", num1.replaceAll(regex, "$1,"));
assertEquals("1,234,567", num2.replaceAll(regex, "$1,"));
assertEquals("12,345,678", num3.replaceAll(regex, "$1,"));
assertEquals("123,456,789", num4.replaceAll(regex, "$1,"));
}
}
The following link suggests AS3 does?
这篇关于插入逗号到数字字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!