Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[10.x] Add charAt method to both Str and Stringable #46349

Merged
merged 3 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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