Skip to content

Commit

Permalink
[10.x] Add charAt method to both Str and Stringable (#46349)
Browse files Browse the repository at this point in the history
* [10.x] Add charAt method to both Str and Stringable

* Update Str.php

* Update Stringable.php

---------

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
localusercamp and taylorotwell committed Mar 5, 2023
1 parent 1ef0471 commit 7626e3b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/Illuminate/Support/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,24 @@ public static function camel($value)
return static::$camelCache[$value] = lcfirst(static::studly($value));
}

/**
* Get the character at the specified index.
*
* @param string $subject
* @param int $index
* @return string|null
*/
public static function charAt($subject, $index)
{
$length = mb_strlen($subject);

if ($index < 0 ? $index < -$length : $index > $length - 1) {
return null;
}

return mb_substr($subject, $index, 1);
}

/**
* Determine if a given string contains a given substring.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Illuminate/Support/Stringable.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@ public function basename($suffix = '')
return new static(basename($this->value, $suffix));
}

/**
* Get the character at the specified index.
*
* @param int $index
* @return string|null
*/
public function charAt($index)
{
return Str::charAt($this->value, $index);
}

/**
* Get the basename of the class path.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportStrTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,16 @@ public function testCamel()
$this->assertSame('fooBarBaz', Str::camel('foo-bar_baz'));
}

public function testCharAt()
{
$this->assertEquals('р', Str::charAt('Привет, мир!', 1));
$this->assertEquals('', Str::charAt('「こんにちは世界」', 4));
$this->assertEquals('w', Str::charAt('Привет, world!', 8));
$this->assertEquals('', Str::charAt('「こんにちは世界」', -2));
$this->assertEquals(null, Str::charAt('「こんにちは世界」', -200));
$this->assertEquals(null, Str::charAt('Привет, мир!', 100));
}

public function testSubstr()
{
$this->assertSame('Ё', Str::substr('БГДЖИЛЁ', -1));
Expand Down
10 changes: 10 additions & 0 deletions tests/Support/SupportStringableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,16 @@ public function testCamel()
$this->assertSame('fooBarBaz', (string) $this->stringable('foo-bar_baz')->camel());
}

public function testCharAt()
{
$this->assertEquals('р', $this->stringable('Привет, мир!')->charAt(1));
$this->assertEquals('', $this->stringable('「こんにちは世界」')->charAt(4));
$this->assertEquals('w', $this->stringable('Привет, world!')->charAt(8));
$this->assertEquals('', $this->stringable('「こんにちは世界」')->charAt(-2));
$this->assertEquals(null, $this->stringable('「こんにちは世界」')->charAt(-200));
$this->assertEquals(null, $this->stringable('Привет, мир!')->charAt('Привет, мир!', 100));
}

public function testSubstr()
{
$this->assertSame('Ё', (string) $this->stringable('БГДЖИЛЁ')->substr(-1));
Expand Down

0 comments on commit 7626e3b

Please sign in to comment.