C++常用字符串操作函数,实现了字符串的分割,前向查找,去除空白等功能
//
// C++常用字符串操作函数的实现
//
//
//
//
// TODO: 等有空了又再补充其他的功能,当前的这几个
// @author norkts<norkts@gmail.com>
// @version 0.1 norkts 2015-04-17 实现了字符串的splits, indexOf, trim, startsWith
//
#include <iostream>
#include <vector>
#include <string>
using namespace std;
namespace norkts{
class StringUtil{
public:
StringUtil(string str){
this->str = str;
}
~StringUtil(){
}
/**
* 分割字符串.
*
* @param string split 分隔符
*/
vector<string> splits(string split){
return splits(str, split);
}
/**
* 返回数组长度 .
*
*/
int length(){
return str.length();
}
/**
* 去除多余的空格
*/
string trim(){
str = trim(str);
return str;
}
/**
* 查找字符串,返回第一个匹配的位置
*/
int indexOf(string search){
return indexOf(str, search);
}
/**
* 是不是以字符串开头
*/
bool startsWith(string search){
return startsWith(str, search);
}
string toString(){
return str;
}
public:
/**
* 同上功能的静态化
*/
static int indexOf(string str, string search){
bool match = false;
for(int i = 0; i < str.length() - search.length(); i++){
for(int j = 0; j < search.length(); j++){
if(search[j] != str[i + j]){
match = false;
break;
}else{
match = true;
}
}
if(match){
return i;
}
}
return -1;
}
/**
* 去除头尾以及连续空白字符合并为1个
*/
static string trim(string str){
while(str[0] == ' '){
memcpy((char *)str.c_str(), str.c_str() + 1, str.length());
}
while(str[str.length() - 1] == ' '){
memset((char *)(str.c_str() + str.length() - 1), '\0', 1);
}
for(int i = 1; i < str.length() - 1; i++){
while(str[i] == ' ' && str[i + 1] == ' '){
memcpy((char *)(str.c_str() + i + 1)
, (str.c_str() + i + 2), str.length() - i - 2);
}
}
return str;
}
/**
* 字符串分割
*/
static vector<string> splits(string str, string split){
char* buff = new char[100];
vector<string> vts;
int pos = 0;
int end = 0;
int slen = split.length();
string tempstr(str.c_str() + pos);
tempstr = trim(tempstr);
end = indexOf (tempstr, split);
while(end > -1){
memset(buff, '\0', 100);
memcpy(buff, tempstr.c_str(), end);
vts.push_back(buff);
cout << buff << endl;
tempstr = string(tempstr.c_str() + end + slen);
tempstr = trim(tempstr);
end = indexOf(tempstr, split);
if(end == -1){
vts.push_back(tempstr);
}
}
return vts;
}
/**
* 判断字符串是不是以search开头
*/
static bool startsWith(string str, string search){
return indexOf(str, search) == 0;
}
private:
string str;
};
};
using namespace norkts;
int main(){
StringUtil str(" driver=Firebird;Uid=SYSDBA; Pwd=masterkey;dbname=D:\\RXHOTEL.FDB; ");
vector<string> res = str.splits(";");
cout << res.size() << endl;
return 0;
}
分享到:
相关推荐
自己封装的纯C++的字符串处理函数大全,像特别好用的 字符串切分 Split函数代码均已经过测试,并有接口说明,可方便调用。
c++字符串操作函数集合整理c++字符串操作函数集合整理
老鼠走迷宫c
C++常用字符串处理函数及使用示例
c++字符串操作函数集合整理c++字符串操作函数集合整理.docx
C++常用库函数 缓冲区操作函数、字符分类函数、数据转换函数、数学函数、输入和输出函数、进程控制函数、字符串操作函数 C++常用库函数 缓冲区操作函数、字符分类函数、数据转换函数、数学函数、输入和输出函数、...
c++字符串操作函数集合整理c++字符串操作函数集合整理借鉴.pdf
C++字符串函数大全.txtC++字符串函数大全.txt
C语言,C++字符串处理函数,涵盖了不少的字符串处理的算法。
c++字符串操作函数集合整理c++字符串操作函数集合整理[借鉴].pdf
C++字符串分割源码,破解版VC助手,兼容多个windows版本
string常用截取字符串方法有很多,但是配合使用以下两种,基本都能满足要求: find(string strSub, npos); find_last_of(string strSub, npos); 其中strSub是需要寻找的子字符串,npos为查找起始位置。找到返回子...
C语言字符串函数大全C/C++基础 函数名: stpcpy 功 能: 拷贝一个字符串到另一个 用 法: char *stpcpy(char *destin, char *source); 程序例: #include <stdio.h> #include <string.h> int main(void) ...
一份示例代码,向你展示了如何通过字符串,调用同名函数
总结了C++中常见的字符串操作函数,是大家在编程中会常遇到的
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考。具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用。 一、用strtok函数进行字符串分割 原型: char *...
本文档包含对C++字符串函数相关内容的资料以及介绍
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。...
数据结构 C++ 字符串 7个操作的基本实现.rar
用C++实现了字符串的倒序,是课堂留的小作业