PHP 的字符串生成器

发布于 2024-10-03 14:06:36 字数 48 浏览 5 评论 0原文

有人用 PHP 实现了 StringBuilder 吗?

Has someone made a StringBuilder implementation in PHP?

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

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

发布评论

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

评论(5

我不吻晚风 2024-10-10 14:06:36

注意:

这个答案是从2010年开始的,现在可能有字符串构建器可以提高性能(从下面的评论来看)。我已经很长时间没有使用 php 了,所以我的知识不是最新的。这个答案可能已经过时了。

以下问题也可能提供有趣的信息,但他们的结论似乎都是相同的。

php 字符串连接,性能


为什么要使用 StringBuilder? PHP 中的字符串是可变的。因此性能不是问题。

像这样构建字符串

$string = "start";
$string .= "appended string";
$string .= "appended string";
etc.

Note:

This answer is from 2010, there might be stringbuilders that can improve the performance by now (judging from the comments below). I have not worked with php for a long time so my knowledge is not up to date. This answer might be out dated.

The following question might provide interesting information as well, all tough their conclusion seem to be the same.

php String Concatenation, Performance


Why do you want to use a StringBuilder? Strings in php are mutable. Therefore performance is not an issue.

Just build string like this

$string = "start";
$string .= "appended string";
$string .= "appended string";
etc.
从﹋此江山别 2024-10-10 14:06:36

您可以使用 sprintf 这只是一个基本版本,但不需要额外的库,示例遵循

$String = "Firstname %s, lastname %s, Age %d";
echo sprintf($String,"Robert","Pitt",22);

并且还处理类型转换和位置替换:

$format = "The %2$s contains %1$d monkeys. That's a nice %2$s full of %1$d monkeys.";
sprintf($format, $num, $location);

尽管我确实喜欢雅各布答案的外观:)

看看t 的强大功能
他的函数和它的姊妹函数在这里:
http://php.net/manual/en/function.sprintf.php

You can use sprintf which is only a basic version but requires no extra libraries, examples Follow

$String = "Firstname %s, lastname %s, Age %d";
echo sprintf($String,"Robert","Pitt",22);

And also handles type casting and position replacements:

$format = "The %2$s contains %1$d monkeys. That's a nice %2$s full of %1$d monkeys.";
sprintf($format, $num, $location);

All though i do like the look of jacob's answer :)

taker a look at the great functionality of t
his function and its sister function here:
http://php.net/manual/en/function.sprintf.php

没︽人懂的悲伤 2024-10-10 14:06:36

有一些实现,但是我不明白为什么你需要 PHP 中的 StringBuilder,至少不是出于性能原因。 PHP 中的纯字符串连接比 sprintf 或 Jacob 建议的实现更快。

There are some implementations out there, however I don't see why you would need a StringBuilder in PHP,at least not for performance reasons. Plain string concatenation in PHP is faster than sprintf or the impelementation Jacob suggested.

ㄖ落Θ余辉 2024-10-10 14:06:36

你不需要 PHP 中的 StringBuilder 或 StringBuffer ,PHP 非常方便,我为你提供,如果你想保留 PyString ,可以使用hereDoc和NowDoc:

$YourString = "start";
$YourString .= <<<'EOD'

appended string
Example of string
spanning multiple lines
using nowdoc syntax.

EOD;

$YourString .= <<<buffer

appended string
Example of string
spanning multiple lines
using heredoc syntax.

appended string

appended string

buffer;

You don't need StringBuilder or StringBuffer in PHP , PHP is super handy I offer you , using from hereDoc and NowDoc if you you wish to keep PyString :

$YourString = "start";
$YourString .= <<<'EOD'

appended string
Example of string
spanning multiple lines
using nowdoc syntax.

EOD;

$YourString .= <<<buffer

appended string
Example of string
spanning multiple lines
using heredoc syntax.

appended string

appended string

buffer;
谢绝鈎搭 2024-10-10 14:06:36

@Amir 的回答给了我灵感,在 PHP 中如果你想要“命名参数”或“位置”参数,你不需要 sprintf,但 HERE_DOC/NOW_DOC 工作得很好。您甚至可以在类内部使用它作为属性并调用getter。

class MyClass{

     private $property;

     private $stock; // some other object with getter 'getSomeProperty()'

     function __toString(){

         $localvar = 'Localvar';
         $localvar2 = 'Localvar2';
         return <<<HERE_DOC
           {{ 
              fqsn: {$this->stock->getSomeProperty()},
              property: {$this->property},
              localvar: {$localvar},
              localvar2: $localvar2
           }}
HERE_DOC;
      } // end __toString()
 } // end MyClass

Answer of @Amir, gave me inspiration to the fact that in PHP if you want 'named parameters' or 'positional' parameters, you don't need sprintf, but HERE_DOC/NOW_DOC works perfect. You can even use this inside of a class for properties and call getters.

class MyClass{

     private $property;

     private $stock; // some other object with getter 'getSomeProperty()'

     function __toString(){

         $localvar = 'Localvar';
         $localvar2 = 'Localvar2';
         return <<<HERE_DOC
           {{ 
              fqsn: {$this->stock->getSomeProperty()},
              property: {$this->property},
              localvar: {$localvar},
              localvar2: $localvar2
           }}
HERE_DOC;
      } // end __toString()
 } // end MyClass
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文