用于 PHP 的 StringBuilder

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4218236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:18:41  来源:igfitidea点击:

StringBuilder for PHP

phpstringbuilder

提问by Mark

Has someone made a StringBuilderimplementation in PHP?

有没有人用StringBuilderPHP 实现过?

回答by Mark Baijens

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.

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



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

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

Just build string like this

像这样构建字符串

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

回答by RobertPitt

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

您可以使用sprintfwhich 只是一个基本版本,但不需要额外的库,示例如下

$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

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

回答by Jan Thom?

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.

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

回答by AMH

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 :

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;

回答by Dimitry K

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 evenuse this inside of a classfor properties and call getters.

@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