| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7158 人关注过本帖, 3 人收藏
标题:[分享]写了个String类,分享一下,欢迎提出建议
取消只看楼主 加入收藏
olivezhang
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-9-14
收藏(3)
 问题点数:0 回复次数:1 
[分享]写了个String类,分享一下,欢迎提出建议

写了个String类, 可以实现==, >, <, [], <<, Trim, Reverse, Find, upper, lower等功能。。。欢迎各位试用,并提出好的建议。

Source Code :

z8N6qK1t.zip (7.92 KB) [分享]写了个String类,分享一下,欢迎提出建议



头文件如下:

#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_


搜索更多相关主题的帖子: String 分享 欢迎 
2006-11-16 17:58
olivezhang
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-9-14
收藏
得分:0 
谢谢各位的支持与鼓励,我会将其再完善。。。:)
kai, 你说是类似于STL中的set,map吗?这个有上定难度哟。。。

谷底深深行 ,峰顶漫漫步......@_@
2006-11-20 14:58
快速回复:[分享]写了个String类,分享一下,欢迎提出建议
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.053361 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved