写了个String类, 可以实现==, >, <, [], <<, Trim, Reverse, Find, upper, lower等功能。。。欢迎各位试用,并提出好的建议。
Source Code :
头文件如下:
#ifndef STRING_H_
#define STRING_H_
//#include <stdlib.h>
//#include "string.h"
#include <iostream>
using namespace std;
/*----------------------------------------------------------------------------*
* name: String
* desc: Process character string.
/----------------------------------------------------------------------------*/
class String
{
public:
String(const char* pStr = NULL);
String(unsigned int nLength);
String(const String& str);
~String();
public:
int GetLength() const;
int GetMemorySize() const;
char* GetString() const;
char GetAt(int nIndex) const;
void SetAt(int nIndex, char ch);
void Reverse();
void Empty();
/*
* name: Delete
* desc: Delete a character or characters in a string start with the
character ai nIndex. If the nCount is longer than the remainder
of the string, only remove the remainder characters of the string.
* in : nIndex - Start character's position.
nCount - Count of characters to be removed.
* out : --
* ret : The length of the changed string.
*/
int Delete(int nIndex, int nCount);
/*
* name: Remove
* desc: Remove instance of ch from the string.
* in : ch - The character to be removed.
* out : --
* ret : The count of characters removed from the string. Zero if the
the string is not changed.
*/
int Remove(char ch);
/*
* name: Insert
* desc: Insert sub-string, String object or a character to the string.
* in : nIndex - The zero-based index of the character in the string.
ch, pStr, str - The character, sub-string and String object to
be inserted.
* out : --
* ret : --
*/
void Insert(int nIndex, char ch);
void Insert(int nIndex, const char* pStr);
void Insert(int nIndex, const String& str);
String Mid(int nFirst) const;
String Mid(int nFirst, int nCount) const;
String Left(int nCount) const;
String Right(int nCount) const;
/*
* name: Find
* desc: Search the string for the first match of a character or a
sub-string.
* in : ch - A single character to search for.
pSubStr - A sub-string to search for.
subStr - A String object to search for.
nStart - The index of character in the string to begin the
search with.
* out : --
* ret : The zero-based index of the first character in this String
object that matches the requested sub-string or character.
-1 if the sub-string or character is not found.
*/
int Find(char ch) const;
int Find(char ch, int nStart) const;
int Find(const char *pSubStr) const;
int Find(const char *pSubStr, int nStart) const;
int Find(const String& subStr) const;
int Find(const String& subStr, int nStart) const;
int ReverseFind(char ch) const;
bool IsEmpty() const;
bool IsAlpha(int nIndex) const;
bool IsNumeric(int nIndex) const;
bool IsLower(int nIndex) const;
bool IsUpper(int nIndex) const;
/*
* name: ToLower
* desc: Change the character indicated by parameter nStart to lower
letter.
* in : nStart - The zero-based index of character in the string.
nCount - The count of characters changed to lower.
* out : --
* ret : --
*/
void ToLower(int nStart, int nCount=1);
/*
* name: ToLower
* desc: Change the characters of the string to lower letter.
* in : --
* out : --
* ret : --
*/
void ToLower();
/*
* name: ToUpper
* desc: Change the character indicated by parameter nStart to upper
letter.
* in : nStart - The zero-based index of character in the string.
nCount - The count of characters changed to upper.
* out : --
* ret : --
*/
void ToUpper(int nStart, int nCount=1);
/*
* name: ToUpper
* desc: Change the all the characters of the string to upper letter.
* in : --
* out : --
* ret : --
*/
void ToUpper();
/*
* name: TrimLeft
* desc: Call the version of function with no parameter to trim
leading whitespace character from the string. When using with
no parameter, TrimLeft trim the newline, whitespace or tab
characters.
Use the version of function that accept parameters to remove
the perticular character or a group of characters from the
beginning of a string.
* in : ch, pSubStr, subStr - A character, sub-string or String object
to be trimed.
* out : --
* ret : --
*/
void TrimLeft();
void TrimLeft(char ch);
void TrimLeft(const char* pSubStr);
void TrimLeft(const String& subStr);
/*
* name: TrimRight
* desc: Call the version of function with no parameter to trim
trailing whitespace character from the string. When using with
no parameter, TrimRight trims the newline, whitespace or tab
characters.
Use the version of function that accept parameters to remove
the perticular character or a group of characters from the
end of a string.
* in : ch, pSubStr, subStr - A character, sub-string or String object
to be trimed.
* out : --
* ret : --
*/
void TrimRight();
void TrimRight(char ch);
void TrimRight(const char* pSubStr);
void TrimRight(const String& subStr);
public:
//Overloaded operator
String& operator =(const String& str);
String operator +(const String& str);
String operator +(const char* pStr);
void operator +=(const String& str);
void operator +=(const char* pStr);
bool operator ==(const String& str);
bool operator >(const String& str);
bool operator <(const String& str);
char operator [](int nIndex);
friend ostream& operator <<(ostream& os, String& str) {
return os << str.m_pData;
}
private:
int StrLen(const char* pStr) const;
char* StrCpy(char* pStrDest, const char* pStrSrc) const;
char* StrCat(char* pStrDest, const char* pStrSrc) const;
/*
* name: StrCmp
* desc: Compare two string.
* in : pStr1, pStr2 -- The factors of comparison.
* out : --
* ret : 0 - pStr1 == pStr2
* >0 - pStr1 > pStr2
* <0 - pStr1 < pStr2
*/
int StrCmp(const char* pStr1, const char* pStr2) const;
bool _IsAlpha(char ch) const;
bool _IsNumeric(char ch) const;
private:
char *m_pData;
int m_nLength;
int m_nMemorySize;
};
#endif //STRING_H_