注册 登录
编程论坛 C++教室

[分享]写了个String类,分享一下,欢迎提出建议

olivezhang 发布于 2006-11-16 17:58, 7172 次点击

写了个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_


26 回复
#2
live412006-11-16 23:36
不错啊,继续努力,加写substring和split和remove等功能呵呵
#3
kai2006-11-17 00:41
olivezhang,

工作的很出色, 很好。

如果有兴趣, 可以开发一些集合类。 在数学上集合是元素组合体的概念。 对于程序开发, 集合可以理解为一个堆放元素的场所。 为什么要开发集合这个类呢? 其意思在我看来就是为了开发数据库系统。 有兴趣的话, 进一步交流。
#4
dragonfly2006-11-17 12:53
挺正规
#5
bobliu5212006-11-18 19:53
顶啊
#6
紫空2006-11-19 18:10
厉害呀,我把它复制了一下,仔细看.
#7
olivezhang2006-11-20 14:58
谢谢各位的支持与鼓励,我会将其再完善。。。:)
kai, 你说是类似于STL中的set,map吗?这个有上定难度哟。。。
#8
flyayi20062006-11-22 13:31
想楼主学习!我的目标!我会赶上各位的,
#9
tancui2006-12-19 20:06
#10
yuyunliuhen2006-12-20 20:59
真的是厉害啊,得多向你们学习学习。
#11
liqiangzk9822006-12-21 00:16
学习,学习,努力学习!向搂主学习。
#12
wopois2007-08-12 20:11
都是英文....
#13
wangweicoin2007-08-12 22:07
有才!!!
#14
cl39225022007-11-15 17:27
没看懂
#15
火乍弓单2007-11-16 13:09
学习!
#16
三月里de小雨2008-01-19 11:56
太猛了点不?
#17
sunkaidong2008-01-19 12:25
学习.类的概念下在用的很多啊.java和mfc里面都有啊.ls最好把注释换汉语啊,不是每个人都看的懂啊.呵呵......
#18
leeco2008-01-21 15:46
回复 3# 的帖子
集合类只不过是对BST的封装罢了,RBT,SBT,AVL,Treap都可以,当然我还是觉得RBT比较快,要不怎么STL用它实现set呢
#19
hgchenkv2008-04-02 09:55
谢谢楼主!
#20
bamboowing2008-08-24 22:20
提个比较低级的问题
==, >, <, [], <<, Trim, Reverse, Find, upper, lower是什么功能啊?
#21
守鹤2008-09-03 22:54
厉害,想一楼楼主学习,看来我要努力了
#22
zhfq2008-09-13 11:27
受教了
#23
zzt_4282008-09-13 16:01
!!!!
果然很牛!
#24
mbstorm2008-11-02 20:25
好厉害
#25
mbstorm2008-11-02 20:25
真是佩服
#26
成就与价值2013-11-12 18:07
楼主厉害!
#27
新手求大神42014-11-29 16:01
w 我是渣渣啊~~~谁能教教我~~~
1