C++ 字串截斷的實現(基礎函式)
C++ 字串截斷的實現
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
/*
* str: 待分隔的原始字串
* pos: 分隔位置
* lor: 擷取字串左側部分(-1) or 右側部分(1)
* */
string cutString(string str, int pos, int lor)
{
if (lor < 0)
{
string s = str;
string ss = s.substr(0, pos);
return ss;
}
if (lor > 0)
{
string s = str;
int len = 0;
len = strlen(s.c_str());
string ss = s.substr(pos, len);
return ss;
}
}
int main()
{
string str = "if you have a dream, you";
string left;
string right;
left = cutString(str, 5, -1);
right = cutString(str, 5, 1);
printf("%s\n", left.c_str());
printf("%s\n", right.c_str());
return 0;
}
相關文章
- c++字串查詢函式實現C++字串函式
- Go基礎-字串函式Go字串函式
- 字串相關函式的實現字串函式
- C語言相關的基礎字串函式C語言字串函式
- PHP內建字串函式實現PHP字串函式
- C++常見函式的基礎演算法C++函式演算法
- Python基礎之openpyxl如何實現vlookup函式Python函式
- JAVA基礎之5-函式式介面的實現Java函式
- python如何截斷字串Python字串
- 函式實現閏年判斷函式
- 【C語言】常用的字串函式及相關函式的自我實現C語言字串函式
- C++分割字串,及strtok函式使用C++字串函式
- 函式基礎函式
- 模擬實現字串函式strlen , strcpy ,strcmp字串函式
- C語言-字串函式的實現(五)之strstrC語言字串函式
- C語言-字串函式的實現(一)之strlenC語言字串函式
- dimp時報錯,字串截斷字串
- SQL-基礎語法 - 截斷和偏移SQL
- 函式基礎和函式引數函式
- python基礎函式Python函式
- Python基礎-函式Python函式
- python函式基礎Python函式
- C++中string字串的基礎操作,學習C++字串
- 虛擬函式表-C++多型的實現原理函式C++多型
- C++模板函式實現型別推導C++函式型別
- c++一些常見的內建函式(字串)C++函式字串
- Go 實現字串首字母大、小寫函式Go字串函式
- strcat函式的基礎認識函式
- python-函式基礎Python函式
- 前端基礎(三):函式前端函式
- 函式指標基礎函式指標
- golang函式使用基礎Golang函式
- python基礎之函式Python函式
- Python基礎(六) 函式Python函式
- Python基礎(10):函式Python函式
- Kotlin基礎之函式Kotlin函式
- c++虛擬函式實現計算表示式子C++函式
- 純 CSS 實現多行文字截斷CSS
- CSS 實現文字"不定行數"截斷CSS