[ c o d e ]
Code here
[ / c o d e ] with out odd spacing gives ya:
Text? Hmm.... You should probably learn the STDLIB for string manipulation - but for myself, I wanted to really dig into the whole string thing myself... to get a handle on it. Now I found I like my string classes better than stdlib... but that's just me... There are bits from stdlib... but I don't use the string class... perhaps you should try the string class. Some googling will definately help.
I have this string class:
jfc_string.h
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
#pragma once
#include <windows.h>
// Stuck this in there for ya... Just commonly used "clean"
// way to delete things from memory... like killing a class...
#ifndef SAFE_DELETE
#define SAFE_DELETE(p) { if(p) { delete (p); (p)=NULL; } }
#endif
//=============================================================================
class JFC_STRING{
//=============================================================================
void pvt_Init(void);
public:
char *s;char chNull;
LONGLONG uiLength;
JFC_STRING();
JFC_STRING(char *p_szDefaultValue);
~JFC_STRING();
void Clear(void);
void Clear(size_t p_NewLength);// allows making a buffer instantly.
void ConCat(const char *p_char);
void Set(const char *p_char);
char *Get(void);
size_t Length(void);
bool EqualCS(char *p_szCompareThis); // Case Sensitive
bool Equal(char *p_szCompareThis);
void Left( char *p_szGrabFromThis, size_t p_HowMany);
void Right( char *p_szGrabFromThis, size_t p_HowMany);
void Mid(const char *p_szGrabFromThis, size_t p_Start, size_t p_HowMany);
void Reverse(const char *p_szGrabFromThis);
void Reverse(void);
void Upper(const char *p_szGrabFromThis);
void Upper(void);
void Lower(const char *p_szGrabFromThis);
void Lower(void);
LONGLONG IndexOf(const char *p_szSearchFor, size_t p_StartHere, bool p_CaseSensitive=false);
LONGLONG SNRChar(const char p_SearchFor, const char p_ReplaceWith, bool p_CaseSensitive=false);
LONGLONG SNR(const char *p_SearchFor, const char *p_ReplaceWith, bool p_CaseSensitive=false);
void ZeroPadInt(int p_i, int p_iHowManyDigits);
};
//=============================================================================
jfc_string.cpp
/*============================================================================
| _________ _______ _______ ______ _______ Jegas, LLC |
| /___ ___// _____/ / _____/ / __ / / _____/ JasonPSage@jegas.com |
| / / / /__ / / ___ / /_/ / / /____ |
| / / / ____/ / / / / / __ / /____ / |
|____/ / / /___ / /__/ / / / / / _____/ / |
/_____/ /______/ /______/ /_/ /_/ /______/ |
| Under the Hood |
==============================================================================
Copyright(c)2008 Jegas, LLC
============================================================================*/
// You can lose this jfc_common.h one...
// providing you keep the "SAFE_DELETE" macro in the
// header I stuffed in there just for you Spring Water :)
#include "jfc_common.h"
#include "jfc_string.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <windows.h>
#include <stdio.h>
//----------------------------------------------------------------------------
JFC_STRING::JFC_STRING(){
//----------------------------------------------------------------------------
this->pvt_Init();
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_STRING::JFC_STRING(char *p_szDefaultValue){
//----------------------------------------------------------------------------
this->pvt_Init();
this->Set(p_szDefaultValue);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::pvt_Init(void){
//----------------------------------------------------------------------------
this->s=0;
chNull='';
this->Clear();
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
JFC_STRING::~JFC_STRING(){
//----------------------------------------------------------------------------
SAFE_DELETE(this->s);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Clear(void){
//----------------------------------------------------------------------------
SAFE_DELETE(this->s);
this->uiLength=0;
this->s=new char[(size_t)this->uiLength+1];
this->s[this->uiLength]='';
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Clear(size_t p_NewLength){
//----------------------------------------------------------------------------
SAFE_DELETE(this->s);
this->uiLength=p_NewLength;
this->s=new char[(size_t)this->uiLength+1];
this->s[this->uiLength]='';
size_t i;
for(i=0;i<this->uiLength;i++){
this->s[i]='';
};
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::ConCat(const char *p_char){
//----------------------------------------------------------------------------
char *st;
this->uiLength=strlen(p_char)+this->uiLength;
size_t NewLen=(size_t)this->uiLength+1;
st=new char[NewLen];
st[0]=chNull;
strcat_s(st,NewLen,this->s);
strcat_s(st,NewLen,p_char);
SAFE_DELETE(this->s);
this->s=new char[NewLen];
strcpy_s(this->s,NewLen,st);
SAFE_DELETE(st);
this->s[this->uiLength]='';
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Set(const char *p_char){
//----------------------------------------------------------------------------
this->Clear();
this->ConCat(p_char);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
char *JFC_STRING::Get(void){
//----------------------------------------------------------------------------
if(this->s!=0){
return this->s;
}else{
return 0;
};
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
size_t JFC_STRING::Length(void){
//----------------------------------------------------------------------------
return (size_t)this->uiLength;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_STRING::EqualCS(char *p_szCompareThis){
//----------------------------------------------------------------------------
return (strcmp(this->s,p_szCompareThis)==0);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
bool JFC_STRING::Equal(char *p_szCompareThis){
//----------------------------------------------------------------------------
return (_strcmpi(this->s,p_szCompareThis)==0);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Left( char *p_szGrabFromThis, size_t p_HowMany){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
JFC_STRING *ts=new JFC_STRING();
ts->Set(p_szGrabFromThis);
this->Clear();
if(p_HowMany>0){
size_t i=0;
char c[2];c[1]=0;
while(i<ts->uiLength && i<p_HowMany){
c[0]=ts->s[i];
this->ConCat(c);
i++;
}
};
SAFE_DELETE(ts);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Right( char *p_szGrabFromThis, size_t p_HowMany){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
JFC_STRING *ts=new JFC_STRING();
ts->Set(p_szGrabFromThis);
this->Clear();
if(p_HowMany>0){
LONGLONG i=ts->uiLength-(LONGLONG)p_HowMany;
if(i<0){
this->Set(ts->s);
}else{
char c[2];c[1]=0;
while(i<ts->uiLength){
c[0]=ts->s[i];
this->ConCat(c);
i++;
}
};
};
SAFE_DELETE(ts);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Mid(const char *p_szGrabFromThis, size_t p_Start, size_t p_HowMany){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
JFC_STRING *ts=new JFC_STRING();
ts->Set(p_szGrabFromThis);
this->Clear();
if(p_Start>=0){
size_t i=p_Start;
char c[2];c[1]=0;
while(i<ts->uiLength && p_HowMany>0){
c[0]=ts->s[i];
this->ConCat(c);
i++;
p_HowMany--;
}
};
SAFE_DELETE(ts);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Reverse(const char *p_szGrabFromThis){
//----------------------------------------------------------------------------
JFC_STRING *ts=new JFC_STRING();
ts->Set(p_szGrabFromThis);
this->Set(_strrev(ts->s));
SAFE_DELETE(ts);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Reverse(void){
//----------------------------------------------------------------------------
JFC_STRING *ts=new JFC_STRING();
ts->Set(_strrev(this->s));
this->Set(ts->s);
SAFE_DELETE(ts);
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Upper(const char *p_szGrabFromThis){
//----------------------------------------------------------------------------
this->Set(p_szGrabFromThis);
this->Upper();
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Upper(void){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
for(size_t i=0;i<this->uiLength;i++){
this->s[i]=(char)toupper(this->s[i]);
};
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Lower(const char *p_szGrabFromThis){
//----------------------------------------------------------------------------
this->Set(p_szGrabFromThis);
this->Lower();
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::Lower(void){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
for(size_t i=0;i<this->uiLength;i++){
this->s[i]=(char)tolower(this->s[i]);
};
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
LONGLONG JFC_STRING::IndexOf(const char *p_szSearchFor, size_t p_StartHere, bool p_CaseSensitive){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
JFC_STRING *ts=new JFC_STRING();
ts->Set(p_szSearchFor);
bool bGotIt=false;
LONGLONG Index=-1;
if(p_StartHere>=0){
for(Index=p_StartHere;Index<this->uiLength;Index++){
bGotIt=true;
LONGLONG i=0;LONGLONG p=0;
for(i=Index;i<this->uiLength && p<ts->uiLength;i++){
if(p_CaseSensitive){
if(this->s[i]!=ts->s[p]){
bGotIt=false;
i=ts->uiLength;//force next outside loop iteration
break;
};
}else{
//char c1=tolower(this->s[i]);
//char c2=tolower(ts->s[p]);
//if(c1!=c2){
if(tolower(this->s[i])!=tolower(ts->s[p])){
bGotIt=false;
i=ts->uiLength;//force next outside loop iteration
};
};
p++;
};
if(bGotIt){
SAFE_DELETE(ts);
return Index;
};
};
};
SAFE_DELETE(ts);
return -1;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
LONGLONG JFC_STRING::SNRChar(const char p_SearchFor, const char p_ReplaceWith, bool p_CaseSensitive){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
LONGLONG Index=-1;
LONGLONG LastIndex=-1;
LONGLONG Changes=0;
char c[2];c[0]=p_SearchFor;c[1]=0;
do{
if(Index>-1){
this->s[Index]=p_ReplaceWith;
Changes+=1;
};
LastIndex=Index;
Index=this->IndexOf(c,0,p_CaseSensitive);
}while(LastIndex!=Index);
return Changes;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
LONGLONG JFC_STRING::SNR(const char *p_SearchFor, const char *p_ReplaceWith, bool p_CaseSensitive){
//----------------------------------------------------------------------------
// TODO: Optimize this - I needed functionality and just cranked out a
// function to do it.
LONGLONG Index=-1;
LONGLONG LastIndex=-1;
JFC_STRING *ts=new JFC_STRING();
JFC_STRING *so=new JFC_STRING(this->s);
LONGLONG Changes=0;
do{
if(Index>-1){
this->Mid(so->s,0,(size_t)Index);
ts->Set(this->s);
size_t Len=strlen(p_SearchFor);
size_t cutend=(size_t)(Index+Len);
this->Mid(so->s,cutend,(size_t)so->uiLength);
ts->ConCat(p_ReplaceWith);
ts->ConCat(this->s);
so->Set(ts->s);
Changes+=1;
};
LastIndex=Index;
Index=so->IndexOf(p_SearchFor,0,p_CaseSensitive);
}while(LastIndex!=Index);
this->Set(so->s);
SAFE_DELETE(so);
SAFE_DELETE(ts);
return Changes;
};
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
void JFC_STRING::ZeroPadInt(int p_i, int p_iHowManyDigits){
//----------------------------------------------------------------------------
char c[50];
for(int i=0;i<50;i++){c[i]='';};
sprintf(c,"%i",p_i);
JFC_STRING *so=new JFC_STRING(c);
so->Reverse();
int ln=so->Length();
for(int i=0;i<p_iHowManyDigits-ln;i++){so->ConCat("0");};
so->Reverse();
this->Set(so->s);
SAFE_DELETE(so);
};
//----------------------------------------------------------------------------
Now before you read this code and freak... (Like you might not know classes good etc).... There is a Cheap Trick I use ALL THE TIME to get values....
First I make a fixed char buffer, and just use it all the time. I make it 1k usually... in fact in my "jfc_common.h" namespace... its named: CHAR1K, so I refer to it like: JFC::CHAR1K.... anyways....
Sample:
sprintf(JFC::CHAR1K,"Poly: %i",dbStatistic(1)); dbPrint(JFC::CHAR1K);
sprintf(JFC::CHAR1K,"Car #: %i",iCar+1);dbPrint(JFC::CHAR1K);
How did I declare CHAR1K? Like this:
char CHAR1K[1024];
So you can place that up top in your code, and drop the whole
JFC:: part... and then that function named:
sprintf becomes your pal
Google it... cuz MSDN help stinks... but you can pass multiple variables... and they show where the format specifiers are, respectfully... %i = integer, %f = float, %s = string... the rest of them and the variations - I'll leave you to google.
--Jason