| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 7158 人关注过本帖, 3 人收藏
标题:[分享]写了个String类,分享一下,欢迎提出建议
只看楼主 加入收藏
olivezhang
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-9-14
收藏(3)
 问题点数:0 回复次数:26 
[分享]写了个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
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
不错啊,继续努力,加写substring和split和remove等功能呵呵
2006-11-16 23:36
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
olivezhang,

工作的很出色, 很好。

如果有兴趣, 可以开发一些集合类。 在数学上集合是元素组合体的概念。 对于程序开发, 集合可以理解为一个堆放元素的场所。 为什么要开发集合这个类呢? 其意思在我看来就是为了开发数据库系统。 有兴趣的话, 进一步交流。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2006-11-17 00:41
dragonfly
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1024
专家分:0
注 册:2006-3-20
收藏
得分:0 
挺正规

2006-11-17 12:53
bobliu521
Rank: 1
等 级:新手上路
帖 子:6
专家分:0
注 册:2006-11-18
收藏
得分:0 
顶啊

博客:http://fengyunzhijia.blog./ 世界上没有免费的午餐
2006-11-18 19:53
紫空
Rank: 1
等 级:新手上路
帖 子:9
专家分:0
注 册:2006-11-19
收藏
得分:0 
厉害呀,我把它复制了一下,仔细看.
2006-11-19 18:10
olivezhang
Rank: 1
等 级:新手上路
帖 子:223
专家分:0
注 册:2005-9-14
收藏
得分:0 
谢谢各位的支持与鼓励,我会将其再完善。。。:)
kai, 你说是类似于STL中的set,map吗?这个有上定难度哟。。。

谷底深深行 ,峰顶漫漫步......@_@
2006-11-20 14:58
flyayi2006
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2006-11-22
收藏
得分:0 
想楼主学习!我的目标!我会赶上各位的,

2006-11-22 13:31
tancui
Rank: 1
等 级:新手上路
威 望:1
帖 子:63
专家分:0
注 册:2006-11-19
收藏
得分:0 
2006-12-19 20:06
yuyunliuhen
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:1435
专家分:0
注 册:2005-12-12
收藏
得分:0 
真的是厉害啊,得多向你们学习学习。

Go confidently in the  directions of your dreams,live the life you have imagined!Just do it!
It is no use learning without thinking!
2006-12-20 20:59
快速回复:[分享]写了个String类,分享一下,欢迎提出建议
数据加载中...
 
   



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

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