程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> c++ 常用字符串處理函數

c++ 常用字符串處理函數

編輯:C++入門知識

c++ 常用字符串處理函數


字符串是目前處理是目前工程項目中出現最多的問題,尤其是自然語言處理,文本處理和分析等等,c++目前只提供比較簡單的字符串處理函數,不像Python,Java對字符串操作功能強大,下面是自己封裝的幾個常用字符處理的函數,其實功能實現應該有很多種,但是由於字符串處理是基礎函數,需要適當的考慮算法實現的性能。下面是自己實現的一些功能,性能感覺還不錯。

 

/*********************************************
function: string common functions.
author: liuyi
date: 2015.08.29
vesion: 1.0
**********************************************/

#ifndef STR_FUN_H
#define STR_FUN_H

#include 
#include 
#include 
#include 
using namespace std;

inline void split_string(const char* src, const string& sep, vector& split_vec)
{
	const char *separator = sep.c_str();
	split_vec.clear();
	split_vec.reserve(16);
	if(src == NULL || separator == NULL)
		return;
	
	const char *p = strstr(src, separator);
	if(NULL == p)
	{
		split_vec.push_back(src);
		return;
	}

	if(p == src)
	{
		split_vec.push_back();
	}
	else
	{
		split_vec.push_back(string(src, p - src));
	}

	size_t len = strlen(separator);
	const char* pre = p + len;
	while(p = strstr(p + len, separator))
	{
		split_vec.push_back(string(pre, p - pre));
		pre = p + len;
	}
	
	if('' == *pre)
	{
		split_vec.push_back();
	}
	else
	{
		split_vec.push_back(string(pre));
	}
}

inline void split_string(const string& src, const string& sep, vector& split_vec)
{
	string::size_type i = src.find(sep);
	if(string::npos == i)
	{
		split_vec.push_back(src);
		return;
	}

	if(0 == i)
	{
		split_vec.push_back();
	}
	else
	{
		split_vec.push_back(src.substr(0, i));
	}

	size_t len = sep.size();
	string::size_type pre = i + len;
	while(string::npos != (i = src.find(sep, pre)))
	{
		split_vec.push_back(src.substr(pre, i - pre));
		pre = i + len;
	}

	split_vec.push_back(src.substr(pre));
}

inline string join_string(const string& sep, const vector& str_vec)
{
	string str;
	str.reserve(32*str_vec.size());

	size_t len = str_vec.size();	
	if(len > 0)
	{
		str = str_vec[0];
	}

	for(size_t i = 1; i < len; i++)
	{
		str.append(sep);
		str.append(str_vec[i]);
	}
	
	return str;
}

inline string replace_string(const string& raw_str, const string& src_str, const string& replace_str)
{
	string new_str;
	new_str.reserve(raw_str.size()*2);
	size_t begin = 0;
	size_t len = src_str.size();
	string::size_type i = raw_str.find(src_str);
	if(string::npos == i)
		return raw_str;
		
	do
	{
		new_str.append(raw_str.substr(begin, i - begin));
		new_str.append(replace_str);
		begin = i + len;
	}
	while(string::npos != (i = raw_str.find(src_str, begin)));

	return new_str.append(raw_str.substr(begin));
}

string lstrip(const string& src, const string& sub_str)
{
	size_t len = sub_str.size();
	string::size_type i = 0;

	i = src.find(sub_str, i);
	int count = 0;
	if(i == 0)
	{
		count = 1;
		while((i = src.find(sub_str, i+len)) != string::npos)
		{
			if(i != len*count)
				break;
			count++;
		}
	}
	else
	{
		return src;
	}

	return src.substr(len*count);
}

string rstrip(const string& str, const string& sub_str)
{
	int sub_len = sub_str.size();
	int last_index = str.size()-1;
	for(int i = last_index; i >= 0; i -= sub_len)
	{
		int flag = 0;
		for(int j = 0; j < i && j < sub_len; j++)
		{
			if(sub_str[j] != str[i + 1 - sub_len + j])
			{
				flag = 1;
				break;
			}
		}
		
		if(flag == 1)
		{
			break;
		}
		else
		{
			last_index = i - sub_len;
		}
	}

	return str.substr(0, last_index+1);
}

inline string strip(const string& src, const string& sub_str)
{
	return lstrip(rstrip(src, sub_str), sub_str);
}

inline int count_of_substr(const string& str, const string& sub_str)
{
	int count = 0;
	size_t len = sub_str.size();
	string::size_type i = 0;
	while((i = str.find(sub_str, i))!= string::npos)
	{
		count++;
		i += len;
	}
	
	return count;
}

inline int count_of_substr(const char *src, const string& sub_str)
{
	int count = 0;
	size_t len = sub_str.size();
	const char *p = sub_str.c_str();
	const char *index = src;
	while(NULL != (index = strstr(index, p)))
	{
		count++;
		index += len;
	}

	return count;
}

string lower_string(const string& src)
{
	string lower_str = src;
	size_t len = src.size();
	for(size_t i = 0; i < len; i++)
	{
		if(src[i] >= 'A' && src[i] <= 'Z')
			lower_str[i] += 32;
	}

	return lower_str;
}

string upper_string(const string& src)
{
	string upper_str = src;
	size_t len = src.size();
	for(size_t i = 0; i < len; i++)
	{
		if(src[i] >= 'a' && src[i] <= 'z')
			upper_str[i] -= 32;
	}

	return upper_str;
}

#endif

#include 
#include 
#include str_fun.h
using namespace std;

int main(int agrc, char *argv[])
{
	char s[16] = {12#34#56};
	vector v;
	split_string(string(s), #, v);
	for(int i = 0; i < v.size(); i++)
		cout<

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved