Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chantsune/py_string
A library that provides methods equivalent to Python 3's str class in C ++
https://github.com/chantsune/py_string
c-plus-plus cpp python string
Last synced: 6 days ago
JSON representation
A library that provides methods equivalent to Python 3's str class in C ++
- Host: GitHub
- URL: https://github.com/chantsune/py_string
- Owner: ChanTsune
- License: mit
- Created: 2018-08-17T12:04:57.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2021-05-06T11:58:11.000Z (over 3 years ago)
- Last Synced: 2024-11-02T14:51:34.907Z (13 days ago)
- Topics: c-plus-plus, cpp, python, string
- Language: C++
- Homepage:
- Size: 231 KB
- Stars: 12
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# py_string
![CI](https://github.com/ChanTsune/py_string/workflows/CI/badge.svg)
A library that provides methods equivalent to Python 3's str class in C ++
A class that reproduced the Python str class## Installation
Since this is a single header library, it is easy to use by include only.
## How to use
Since it inherits std::string, it can be handled like std::string. Think that you can use python's str type method with std::string.
```cpp
#include "py_string.hpp"
```### multiplication
```cpp
py::string str = "hello world!";std::cout << str * 2 << std::endl;
//hello world!hello world!
```### formatting for title style
```cpp
std::cout << str.title() << std::endl;
//Hello World!
```### splitting
```cpp
py::string str2 = "12,3,4-2,2,3";
std::deque slist; //std::vector is also acceptablestr2.split(slist, ",");
for (auto &&s : slist)
{
std::cout << s << std::endl;
}
//12
//3
//4-2
//2
//3
```### formatting
```cpp
py::string str3 = "{0} format string{1}";
std::cout << str3.format("let's", "!") << std::endl;
//let's format string!
py::string str4 = "{}{}{}";
std::cout << str4.format("1", 2, "3") << std::endl;
//123py::string str = "{0},{1},{0}";
std::cout << str.format("apple", "banana") << std::endl;
//apple,banana,apple```
### string slicing
```cpp
py::string str5 = "0123456789";
std::cout << str5[{0,5}] << std::endl;
//01234
std::cout << str5[{py::nullopt, py::nullopt, -1}] << std::end;
//9876543210
```### reverse order access
```cpp
std::cout << str5[-1] << std::endl;
//9
std::cout << str5[-3] << std::endl;
//7
```For more detailed usage of this library, please refer to example.cpp.
The behavior of each method is almost the same as that of python's str type.
Please see python's official support for details.https://docs.python.org/3/library/stdtypes.html#string-methods