PHP code example of greg0 / string-builder

1. Go to this page and download the library: Download greg0/string-builder library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

greg0 / string-builder example snippets


$sb = new StringBuilder('Initial string');
$sb->append(' appended string');
$sb->appendLine();
$sb->appendLine('Other paragraph');
$sb->appendFormat('%s: %d', 'Value', 23);
$sb->appendLine();
$sb->append('End of poem.');

echo $sb->toString(); // echo (string)$sb;

$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o.o');

echo $sb->toString(); // ---[o.o]---

$sb = new StringBuilder('---[]---');
$sb->insert(4, 'o', 2);

echo $sb->toString(); // ---[oo]---

$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->remove(6, 5); // remove "ipsum"
echo $sb->toString(); // Lorem  dolor sit amet.

$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->replace('ipsum', 'lirum');
echo $sb->toString(); // Lorem lirum dolor sit amet.

$sb = new StringBuilder('Lorem ipsum dolor sit amet.');
$sb->clear();
echo $sb->toString(); // will return empty string