diff --git a/src/Tempest/Support/src/StringHelper.php b/src/Tempest/Support/src/StringHelper.php index a3fc0af91..eea1b3ab7 100644 --- a/src/Tempest/Support/src/StringHelper.php +++ b/src/Tempest/Support/src/StringHelper.php @@ -182,6 +182,16 @@ public function finish(string $cap): self ); } + /** + * Prefixes the instance with the given string. + */ + public function start(string $prefix): self + { + return new self( + $prefix.preg_replace('/^(?:'.preg_quote($prefix, '/').')+/u', replacement: '', subject: $this->string) + ); + } + /** * Returns the remainder of the string after the first occurrence of the given value. */ diff --git a/src/Tempest/Support/tests/StringHelperTest.php b/src/Tempest/Support/tests/StringHelperTest.php index 4eba1a3bd..ed95ad89f 100644 --- a/src/Tempest/Support/tests/StringHelperTest.php +++ b/src/Tempest/Support/tests/StringHelperTest.php @@ -475,4 +475,10 @@ public function test_unwrap(): void $this->assertSame('value', str('value]')->unwrap('[', ']', strict: false)->toString()); $this->assertSame('Scott', str('Scott Kennedy')->unwrap(before: 'Leon ', after: ' Kennedy', strict: false)->toString()); } + + public function test_start(): void + { + $this->assertSame('Leon Scott Kennedy', str('Scott Kennedy')->start('Leon ')->toString()); + $this->assertSame('Leon Scott Kennedy', str('Leon Scott Kennedy')->start('Leon ')->toString()); + } }