Skip to content

Commit 7626e3b

Browse files
[10.x] Add charAt method to both Str and Stringable (#46349)
* [10.x] Add charAt method to both Str and Stringable * Update Str.php * Update Stringable.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 1ef0471 commit 7626e3b

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

src/Illuminate/Support/Str.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,24 @@ public static function camel($value)
216216
return static::$camelCache[$value] = lcfirst(static::studly($value));
217217
}
218218

219+
/**
220+
* Get the character at the specified index.
221+
*
222+
* @param string $subject
223+
* @param int $index
224+
* @return string|null
225+
*/
226+
public static function charAt($subject, $index)
227+
{
228+
$length = mb_strlen($subject);
229+
230+
if ($index < 0 ? $index < -$length : $index > $length - 1) {
231+
return null;
232+
}
233+
234+
return mb_substr($subject, $index, 1);
235+
}
236+
219237
/**
220238
* Determine if a given string contains a given substring.
221239
*

src/Illuminate/Support/Stringable.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public function basename($suffix = '')
9999
return new static(basename($this->value, $suffix));
100100
}
101101

102+
/**
103+
* Get the character at the specified index.
104+
*
105+
* @param int $index
106+
* @return string|null
107+
*/
108+
public function charAt($index)
109+
{
110+
return Str::charAt($this->value, $index);
111+
}
112+
102113
/**
103114
* Get the basename of the class path.
104115
*

tests/Support/SupportStrTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,16 @@ public function testCamel()
749749
$this->assertSame('fooBarBaz', Str::camel('foo-bar_baz'));
750750
}
751751

752+
public function testCharAt()
753+
{
754+
$this->assertEquals('р', Str::charAt('Привет, мир!', 1));
755+
$this->assertEquals('', Str::charAt('「こんにちは世界」', 4));
756+
$this->assertEquals('w', Str::charAt('Привет, world!', 8));
757+
$this->assertEquals('', Str::charAt('「こんにちは世界」', -2));
758+
$this->assertEquals(null, Str::charAt('「こんにちは世界」', -200));
759+
$this->assertEquals(null, Str::charAt('Привет, мир!', 100));
760+
}
761+
752762
public function testSubstr()
753763
{
754764
$this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1));

tests/Support/SupportStringableTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,16 @@ public function testCamel()
936936
$this->assertSame('fooBarBaz', (string) $this->stringable('foo-bar_baz')->camel());
937937
}
938938

939+
public function testCharAt()
940+
{
941+
$this->assertEquals('р', $this->stringable('Привет, мир!')->charAt(1));
942+
$this->assertEquals('', $this->stringable('「こんにちは世界」')->charAt(4));
943+
$this->assertEquals('w', $this->stringable('Привет, world!')->charAt(8));
944+
$this->assertEquals('', $this->stringable('「こんにちは世界」')->charAt(-2));
945+
$this->assertEquals(null, $this->stringable('「こんにちは世界」')->charAt(-200));
946+
$this->assertEquals(null, $this->stringable('Привет, мир!')->charAt('Привет, мир!', 100));
947+
}
948+
939949
public function testSubstr()
940950
{
941951
$this->assertSame('Ё', (string) $this->stringable('БГДЖИЛЁ')->substr(-1));

0 commit comments

Comments
 (0)