简体   繁体   English

在 Dart 中连接字符串的最有效方法是什么?

[英]What is the most efficient way to concatenate strings in Dart?

Languages like Java, let you concatenate Strings using '+" operator. Java 等语言允许您使用“+”运算符连接字符串。

But as strings are immutable, they advise one to use StringBuilder for efficiency if one is going to repeatedly concatenate a string.但由于字符串是不可变的,如果要重复连接字符串,他们建议使用 StringBuilder 以提高效率。

What is the most efficient way to concatenate Strings in Dart?在 Dart 中连接字符串的最有效方法是什么?

https://api.dart.dev/stable/2.9.1/dart-core/StringBuffer-class.html https://api.dart.dev/stable/2.9.1/dart-core/StringBuffer-class.html

StringBuffer can be used for concatenating strings efficiently.

Allows for the incremental building of a string using write*() methods. The strings are concatenated to a single string only when toString is called.

It appears that if one uses StringBuffer, one is postponing the performance hit till toString is called?看来,如果使用 StringBuffer,则将性能影响推迟到调用 toString 之前?

There are a number of ways to concatenate strings:有多种连接字符串的方法:

  • String.operator + : string1 + string2 . String.operator + : string1 + string2 This is the most straightforward.这是最直接的。 However, if you need to concatenate a lot of strings, using + repeatedly will create a lot of temporary objects, which is inefficient.但是如果需要拼接很多字符串,重复使用+会创建很多临时对象,效率很低。 (Also note that unlike other concatenation methods, + will throw an exception if either argument is null .) (另请注意,与其他连接方法不同,如果任一参数为null+将引发异常。)

  • String interpolation: '$string1$string2' .字符串插值: '$string1$string2' If you need to concatenate a fixed number of strings that are known in advance (such that you can use a single interpolating string), I would expect this to be reasonably efficient.如果您需要连接预先知道的固定数量的字符串(以便您可以使用单个插值字符串),我希望这会相当有效。 If you need to incrementally build a string, however, this would have the same inefficiency as + .但是,如果您需要增量构建一个字符串,这将具有与+相同的低效率。

  • StringBuffer . StringBuffer This is efficient if you need to concatenate a lot of strings.如果您需要连接很多字符串,这很有效。

  • Iterable.join : [string1, string2].join() . Iterable.join : [string1, string2].join() This internally uses a StringBuffer so would be equivalent.这在内部使用StringBuffer ,因此是等效的。

If you need to concatenate a small, fixed number of strings, I would use string interpolation.如果您需要连接少量固定数量的字符串,我会使用字符串插值。 It's usually more readable than using + , especially if there are string literals involved.它通常比使用+更具可读性,尤其是在涉及字符串文字的情况下。 Using StringBuffer in such cases would add some unnecessary overhead.在这种情况下使用StringBuffer会增加一些不必要的开销。

Here is what I understood:这是我的理解:

It appears that performance will vary depending on your use case.似乎性能会因您的用例而异。 If you use StringBuffer, and intend to concatenate a very large string, then as the concatenation occurs only when you call toString, it is at that point where you get the "performance hit".如果您使用 StringBuffer,并打算连接一个非常大的字符串,那么由于连接仅在您调用 toString 时发生,因此您将获得“性能打击”。

But if you use "+", then each time you call "+" there is a performance hit.但是如果你使用“+”,那么每次调用“+”都会对性能造成影响。 As strings are immutable, each time you concatenate two strings you are creating a new object which needs to be garbage collected.由于字符串是不可变的,因此每次连接两个字符串时,您都会创建一个新的 object,需要对其进行垃圾收集。

Hence the answer seems to be to test for your situation, and determine which option makes sense.因此,答案似乎是测试您的情况,并确定哪个选项有意义。

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

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