C + + Common string processing functions

Source: Internet
Author: User

String is currently processing is currently the most problems in the project, especially natural language processing, text processing and analysis, and so on, C + + currently only provides a relatively simple string processing function, unlike Python,java to string manipulation function, the following is their own package of several characters commonly used character processing functions, In fact, there should be many kinds of function implementation, but because the string processing is the basic function, it is necessary to consider the performance of the algorithm implementation properly. Here are some of the features you have implemented, and the performance feels good.

/*********************************************function:string Common Functions.author:liuyidate:2015.08.29vesion : 1.0**********************************************/#ifndef str_fun_h#define str_fun_h#include <iostream># Include <string.h> #include <string> #include <vector>using namespace std;inline void Split_string ( Const char* SRC, const string& Sep, vector<string>& split_vec) {const char *separator = SEP.C_STR (); split_ve  C.clear (); Split_vec.reserve (+); 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));p re = P + len;} if (' \ n ' = = *pre) {split_vec.push_back ("");} Else{split_vec.push_back (String (pre));}} inline void split_string (const string& SRC, Const STring& Sep, vector<string>& 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_bac K (Src.substr (pre, i-pre));p re = i + len;} Split_vec.push_back (SRC.SUBSTR (pre));} Inline string join_string (const string& Sep, const vector<string>& 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 ()); 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 = sr C.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*co UNT) 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 (); str Ing::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 CHA R *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 <iostream> #include <stdlib.h> #include "str_fun.h" using namespace Std;int main (int agrc, char *argv []) {char s[16] = {"12#34#56"};vector<string> v;split_string (String (s), "#", v); for (int i = 0; i < v.size (); i++) c Out<<v[i]<<endl;cout<<join_string ("#", V) <<endl;cout<<replace_string (S, "34", "") <<endl;cout<<count_of_substr ("# #", "#") <<endl;cout<<lower_string ("AbC") <<endl; Cout<<upper_string ("AbC") <<endl;cout<<lstrip ("Aaaabc", "AaB") <<endl;cout<<rstrip ( "Abcabcabc", "ABCD") <<endl;cout<<strip ("Abcabcaba", "a") <<endl;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

C + + Common string processing functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.