VB icon

STring utils

Email
Submitted on: 7/31/2016 8:07:27 AM
By: Thomas1965  
Level: Beginner
User Rating: By 1 Users
Compatibility: C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 162
 
     Bunch of functions for string processing - split, format, trim. Uses std::string and should work with any C++ compiler.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: STring utils
// Description:Bunch of functions for string processing - split, format, trim.
Uses std::string and should work with any C++ compiler.
// By: Thomas1965
//**************************************

//StringUtils.h
#ifndef STRUTILS_H
#define STRUTILS_H
#include <string>
#include <vector>
using std::string;
using std::vector;
typedef vector<string> StringVector;
namespace StrUtils
{
 string ReplaceString(const string& src, const string& old, string replace);
 string FormatString(char *fmt, ...);
 bool SplitString(const string& src, char sep, string& lhs, string& rhs);
 void SplitString(const string& src, char sep, StringVector& output);
 void SplitString(const string& src, string separators, StringVector& output);
 string TrimLeft(const string& src, char ch = ' ');
 string TrimRight(const string& src, char ch = ' ');
 string Trim(const string& src, char ch = ' ');
}
#endif
// StrUtils.cpp
#include <stdarg.h>
#include <stdio.h>
#include "StrUtils.h"
namespace StrUtils
{
 string ReplaceString(const string& src, const string& old, string replace)
 {
if (src.empty() || old.empty())
 return string(src);
string::size_type pos;
string s(src);
while((pos = s.find(old)) != string::npos)
{
 s.replace(pos, old.size(), replace); 
}
return s;
 }
 string FormatString(char *fmt, ...)
 {
const int BUF_LEN = 1024;
char buf[BUF_LEN];
va_list start;
va_start(start, fmt);
_vsnprintf(buf, BUF_LEN, fmt, start);
va_end(start);
return string(buf);
 }
 bool SplitString(const string& src, char sep, string& lhs, string& rhs)
 {
if (src.size() < 3) // sep + min key + min value
 return false;
 
string::size_type pos = src.find(sep); 
if (pos == 0 || pos >= src.size()-1)
 return false;
lhs.assign(src, 0, pos);
rhs.assign(src, pos+1, string::npos);
return true;
 }
 void SplitString(const string& src, char sep, StringVector& output)
 {
string::size_type start_pos = src.find_first_not_of(sep);
string::size_type end_pos;
while (start_pos != string::npos)
{ 
 end_pos = src.find_first_of(sep, start_pos);
 if (end_pos == string::npos)
 {
end_pos = src.size();
 }
 string s(src, start_pos, end_pos - start_pos);
 output.push_back(s);
 start_pos = src.find_first_not_of(sep, end_pos);
}
 }
 void SplitString(const string& src, string separators, StringVector& output)
 {
string::size_type start_pos = src.find_first_not_of(separators);
string::size_type end_pos;
while (start_pos != string::npos)
{ 
 end_pos = src.find_first_of(separators, start_pos);
 if (end_pos == string::npos)
 {
end_pos = src.size();
 }
 string s(src, start_pos, end_pos - start_pos);
 output.push_back(s);
 start_pos = src.find_first_not_of(separators, end_pos);
}
 }
 string TrimLeft(const string& src, char ch)
 {
string retval;
string::size_type i = 0, len = src.size();
while(src[i] == ch && i < len)
 i++;
if (i == src.size())
 return "";
return string (src, i, string::npos);
 }
 string TrimRight(const string& src, char ch)
 {
if (src.empty())
 return src;
string::size_type j = src.size()-1;
while (j > 0 && src[j] == ch)
 j--;
if (j == 0)
 return "";
return string(src, 0, j+1);;
 }
 string Trim(const string& src, char ch)
 {
if (src.empty())
 return src;
string::size_type i = 0 , j = src.size() -1;
while (i < j && src[i] == ch)
 i++;
while (j > i && src[j] == ch)
 j--;
if (i == j)
 return "";
return string(src, i, j-i+1);
 }
}


Other 15 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/31/2016 10:02:48 AMBen

Nise code I started my own project like this to cover most of the vb6 functions to C, but still missing some things I upload soon. But your project is good.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.