将 string.Split(',') 转换为 int 数组

发布于 2024-11-25 09:32:08 字数 313 浏览 2 评论 0原文

我有以下字符串:

"2,26,17,33,6,14,9,29,1"

和一个 int, usedIds< 数组/代码>。

如果我这样做:

private var usedIds:Array;
usedIds = "2,26,17,33,6,14,9,29,1".split(',');

我会得到一个字符串数组。

我怎样才能获得int数组?

I have the following string:

"2,26,17,33,6,14,9,29,1"

And an array of int, usedIds.

If I do:

private var usedIds:Array;
usedIds = "2,26,17,33,6,14,9,29,1".split(',');

I get an array of strings.

How can I do it to get an array of int?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提赋 2024-12-02 09:32:08

最简单的方法是使用 Linq:

int[] usedIds = "2,26,17,33,6,14,9,29,1".split(',').Select(x => int.Parse(x)).ToArray();

The simplest way is using Linq:

int[] usedIds = "2,26,17,33,6,14,9,29,1".split(',').Select(x => int.Parse(x)).ToArray();
递刀给你 2024-12-02 09:32:08

您可以尝试以下操作:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var string:String = "2,26,17,33,6,14,9,29,1";

            var strings:Array = string.split(',');

            var ints:Array = new Array();

            for each(var s:String in strings)
            {
                ints.push(int(s));

            }// end for each

            trace(ints[0]) // output: 2
            trace(ints[0] is int) // output: true

        }// end function

    }// end class

}// end package

[UPDATE]

上面的稍短版本是:

var string:String = "2,26,17,33,6,14,9,29,1";

var ints:Array = new Array();

for each(var s:String in string.split(","))
{
    ints.push(int(s));

}// end for each

trace(ints[0]) // output: 2
trace(ints[0] is int) // output: true

[UPDATE 2]

最短(不推荐):

var ints:Array = [];

for each(var s:String in "2,26,17,33,6,14,9,29,1".split(",")) ints.push(int(s));

trace(ints[0]) // output: 2
trace(ints[0] is int) // output: true

You could try this:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var string:String = "2,26,17,33,6,14,9,29,1";

            var strings:Array = string.split(',');

            var ints:Array = new Array();

            for each(var s:String in strings)
            {
                ints.push(int(s));

            }// end for each

            trace(ints[0]) // output: 2
            trace(ints[0] is int) // output: true

        }// end function

    }// end class

}// end package

[UPDATE]

A slightly shorter version of the above is:

var string:String = "2,26,17,33,6,14,9,29,1";

var ints:Array = new Array();

for each(var s:String in string.split(","))
{
    ints.push(int(s));

}// end for each

trace(ints[0]) // output: 2
trace(ints[0] is int) // output: true

[UPDATE 2]

Shortest(don't recommend):

var ints:Array = [];

for each(var s:String in "2,26,17,33,6,14,9,29,1".split(",")) ints.push(int(s));

trace(ints[0]) // output: 2
trace(ints[0] is int) // output: true
夜雨飘雪 2024-12-02 09:32:08

另一个例子:

var a:Array = "2,26,17,33,6,14,9,29,1".split(",");
a.forEach(function(item:*, index:int, array:Array):void{
   array[index] = int(item);
});

An other example :

var a:Array = "2,26,17,33,6,14,9,29,1".split(",");
a.forEach(function(item:*, index:int, array:Array):void{
   array[index] = int(item);
});
壹場煙雨 2024-12-02 09:32:08

您可能必须循环遍历数组并将其转换(已测试):

function splitToInt($input:String,$delimiter:String):Array
{
    var inArr:Array = $input.split($delimiter);
    var outArr:Array = [];
    for each(var index in inArr) {
        trace(index);
        outArr.push(int(index));
    }
    return outArr;  
}

var usedIds:Array = splitToInt("2,26,17,33,6,14,9,29,1",",");

You might have to loop through your array and convert it (tested):

function splitToInt($input:String,$delimiter:String):Array
{
    var inArr:Array = $input.split($delimiter);
    var outArr:Array = [];
    for each(var index in inArr) {
        trace(index);
        outArr.push(int(index));
    }
    return outArr;  
}

var usedIds:Array = splitToInt("2,26,17,33,6,14,9,29,1",",");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文