Skip to content

Commit 00f1643

Browse files
author
Martin Brecht-Precht
committed
Added lastChar and firstChar methods.
1 parent 66d6822 commit 00f1643

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/StringBuilder.php

+20
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,26 @@ public function charAt($position)
261261
return mb_substr($this->string, $position, 1);
262262
}
263263

264+
/**
265+
* Returns first character
266+
*
267+
* @return string
268+
*/
269+
public function firstChar()
270+
{
271+
return mb_substr($this->string, 0, 1);
272+
}
273+
274+
/**
275+
* Returns last character
276+
*
277+
* @return string
278+
*/
279+
public function lastChar()
280+
{
281+
return mb_substr($this->string, -1);
282+
}
283+
264284
/**
265285
* Returns an substring defined by startPosition and length
266286
*

test/StringBuilderTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function testBuilder()
3939
$this->assertEquals(9, $builder->lastIndexOf('e'));
4040
$this->assertNull($builder->lastIndexOf('e',10));
4141
$this->assertTrue($builder->contains('21b'));
42+
$this->assertEquals('b', $builder->firstChar());
43+
$this->assertEquals('ö', $builder->lastChar());
4244
$this->assertFalse($builder->contains('abc'));
4345
$this->assertEquals('öeb1212ewqb', $builder->reverse()->build());
4446
$builder = new StringBuilder('0123456');

0 commit comments

Comments
 (0)