Класс ByteString
Класс ByteString из пространства имён
Symfony\Component\String представляет
бинарную строку. В отличие от UnicodeString,
класс ByteString работает с байтами,
а не с символами Unicode. Конструктор принимает
строку. Класс полезен для работы с бинарными
данными: хешами, зашифрованными строками,
содержимым файлов.
Синтаксис
use Symfony\Component\String\ByteString;
$string = new ByteString(string $data);
Пример
Давайте создадим объект ByteString
и выведем его на экран:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\String\ByteString;
class ArticleController extends AbstractController
{
#[Route('/', name: 'article_index')]
public function index(): Response
{
$string = new ByteString('abcde');
return new Response($string->toString());
}
}
?>
Результат выполнения кода:
abcde
Пример
Давайте получим длину строки с помощью метода
length:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\String\ByteString;
class ArticleController extends AbstractController
{
#[Route('/', name: 'article_index')]
public function index(): Response
{
$string = new ByteString('abcde');
$res = $string->length();
return new Response((string) $res);
}
}
?>
Результат выполнения кода:
5
Пример
Давайте вырежем часть строки методом
slice:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\String\ByteString;
class ArticleController extends AbstractController
{
#[Route('/', name: 'article_index')]
public function index(): Response
{
$string = new ByteString('abcde');
$res = $string->slice(1, 3);
return new Response($res->toString());
}
}
?>
Результат выполнения кода:
bcd
Пример
Давайте сгенерируем случайную строку с помощью
статического метода fromRandom:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\String\ByteString;
class ArticleController extends AbstractController
{
#[Route('/', name: 'article_index')]
public function index(): Response
{
$string = ByteString::fromRandom(8);
return new Response($string->toString());
}
}
?>
Результат выполнения кода:
"a1b2c3d4"
Пример
Давайте преобразуем ByteString
в UnicodeString методом
toUnicodeString:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\String\ByteString;
class ArticleController extends AbstractController
{
#[Route('/', name: 'article_index')]
public function index(): Response
{
$byteString = new ByteString('hello');
$unicodeString = $byteString->toUnicodeString();
$res = $unicodeString->upper();
return new Response($res->toString());
}
}
?>
Результат выполнения кода:
HELLO
Смотрите также
-
класс
UnicodeString,
который работает с символами Unicode -
метод
fromRandom,
который создаёт случайную строку -
метод
toUnicodeString,
который преобразует байтовую строку в Unicode -
функция
u,
которая создаёт UnicodeString из строки