記錄cpp相關的字符串操作
#include <string>
C++中的std::string
可以視爲std::vector<char>
類型的向量,因此可以用.size()
函數返回字符串的字符長度(不含'\n'結尾符號)。
以下是string
相關的常見操作
- 反轉
- 字符串相加
- 轉爲int型:利用
stoi
將字符串轉爲整型(https://www.geeksforgeeks.org/stdstoi-function-in-cpp/)
- 同float
- C++ 字符串split
int idx = str.find(' ')
如有多個字符該如何,返回第一次出現的位置嗎
str.substr(idx, length)
起始座標+子串長度
字符子串
用到的函數
#include <string>
idx = str.find_last_of
str.substr(strIdx, length)
舊電腦上應該保存一些code snippet