简体   繁体   English

如何在Haxe中乘以字符串

[英]How to multiply strings in Haxe

I'm trying to multiply some string a by some integer b such that a * b = a + a + a... (b times) . 我试图将一些字符串a乘以某个整数b ,使a * b = a + a + a ...(b次) I've tried doing it the same way I would in python: 我尝试过就像在python中一样:

class Test {
    static function main() {
        var a = "Text";
        var b = 4;    
        trace(a * b);    //Assumed Output: TextTextTextText
    }
}

But this raises: 但这引起了:

Build failure Test.hx:6: characters 14-15 : String should be Int 构建失败Test.hx:6:字符14-15:字符串应为Int

There doesn't seem to be any information in the Haxe Programming Cookbook or the API Documentation about multiplying strings, so I'm wondering if I've mistyped something or if I should use: 在Haxe Programming Cookbook或API Documentation中似乎没有任何关于乘法字符串的信息,所以我想知道我输错了什么或者我应该使用:

class Test {
    static function main() {
        var a = "Text";
        var b = 4;
        var c = ""; 

        for (i in 0...b) {
            c = c + a;
        }
        trace(c); // Outputs "TextTextTextText"
    }
}

The numeric multiplication operator * requires numeric types, like integer. 数字乘法运算符*需要数字类型,如整数。 You have a string. 你有一个字符串。 If you want to multiply a string, you have to do it manually by appending a target string within the loop. 如果要将字符串相乘,则必须通过在循环中追加目标字符串来手动执行此操作。

The + operator is not the numeric plus in your example, but a way to combine strings. +运算符不是示例中的数字加号,而是组合字符串的一种方法。

You can achieve what you want by operator overloading : 你可以通过运算符重载实现你想要的:

abstract MyAbstract(String) {
    public inline function new(s:String) {
        this = s;
    }

    @:op(A * B)
    public function repeat(rhs:Int):MyAbstract {
        var s:StringBuf = new StringBuf();
        for (i in 0...rhs)
            s.add(this);
        return new MyAbstract(s.toString());
    }
}

class Main {
    static public function main() {
        var a = new MyAbstract("foo");
        trace(a * 3); // foofoofoo
    }
}

Not very short, but array comprehension might help in some situations : 不是很短,但阵列理解可能在某些情况下有所帮助:

class Test {
    static function main() {
        var a = "Text";
        var b = 4;
        trace( [for (i in 0...b) a].join("") );
        //Output: TextTextTextText
    }
}

See on try.haxe.org . 请参阅try.haxe.org

To build on tokiop's answer , you could also define a times function, and then use it as a static extension. 要构建tokiop的答案 ,您还可以定义times函数,然后将其用作静态扩展。

using Test.Extensions;

class Test {
    static function main() {
        trace ("Text".times(4));
    }
}

class Extensions {
    public static function times (str:String, n:Int) {
        return [for (i in 0...n) str].join("");
    }
}

try.haxe.org demo here 在这里试试.haxe.org演示

To build on bsinky answer, you can also define a times function as static extension, but avoid the array: 要构建bsinky答案,您还可以将时间函数定义为静态扩展,但避免使用数组:

using Test.Extensions;

class Test {
    static function main() {
        trace ("Text".times(4));
    }
}

class Extensions {
    public static function times (str:String, n:Int) {
        var v = new StringBuf();
        for (i in 0...n) v.add(str);
        return v.toString();
    }
}

Demo: https://try.haxe.org/#e5937 演示: https//try.haxe.org/#e5937

StringBuf may be optimized for different targets. StringBuf可以针对不同的目标进行优化。 For example, on JavaScript target it is compiled as if you were just using strings https://api.haxe.org/StringBuf.html 例如,在JavaScript目标上,它被编译为好像您只是使用字符串https://api.haxe.org/StringBuf.html

The fastest method (at least on the JavaScript target from https://try.haxe.org/#195A8 ) seems to be using StringTools._pad . 最快的方法(至少在https://try.haxe.org/#195A8的JavaScript目标上)似乎使用StringTools._pad

public static inline function stringProduct ( s : String, n : Int ) {

    if ( n < 0 ) {

        throw ( 1 );

    }

    return StringTools.lpad ( "", s, s.length * n );

}

StringTools.lpad and StringTools.rpad can't seem to decide which is more efficient. StringTools.lpadStringTools.rpad似乎无法决定哪个更有效。 It looks like rpad might be better for larger strings and lpad might be better for smaller strings, but they switch around a bit with each rerun. 看起来rpad对于较大的字符串可能更好,而对于较小的字符串,lpad可能更好,但是每次重新运行时它们会稍微切换一下。 haxe.format.JsonPrinter uses lpad for concatenation, but I'm not sure which to recommend. haxe.format.JsonPrinter使用lpad进行连接,但我不确定要推荐哪个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM