📄 string.h
字号:
// String.h: interface for the String class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_STRING_H__B4BE3995_5704_4EA6_890F_85E088401091__INCLUDED_)
#define AFX_STRING_H__B4BE3995_5704_4EA6_890F_85E088401091__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
//Fundamental Functions of char array
int Strlen(char *str) //Get the length of char array
{
int length=0;
for (;*str++;length++);
return length;
}
void Strcpy(char *str1, char *str2) //Char array copy
{
for (;*str2;*str1++=*str2++);
*str1=0;
}
void Strcat(char *str1, char *str2) //strcat
{
for (;*str1;str1++); //Get to the tail of str1
Strcpy(str1,str2);
}
int Strcmp(char *str1, char *str2) //Cmpare two char arrays
{
for (;*str1 && *str2;str1++,str2++)
{
if (*str1==*str2)
continue;
else if (*str1>*str2)
return 1;
else
return -1;
}
if (*str1)
return 1;
else if (*str2)
return -1;
return 0;
}
//String begin
class String
{
private:
char *myString;
int length;
public: //Constructor/Destructor
String();
String(char*);
String(String&); //Copy Constructor
~String();
public: //operator overload
String operator = (char *); //Assign
String operator = (String&); //Assign
friend istream& operator >> (istream& in, String& str) //input
{
str=""; //No matter what the str is originally, clear it
char c;
char ch[2];
bool firstInput=true; //judge whether is the first input
while(c=in.get()) //While the input is not enter
{
if (c=='\n' || c==' ' || c==9)
{
if ((c==' ' || c==9) && firstInput) //If is the first input, ignore the space
continue;
break;
}
ch[0]=c;
ch[1]=0;
str+=ch;
firstInput=false; //not the space, change the value of firstInput
}
return in;
}
friend ostream& operator << (ostream& out, String& str) //output
{
out<<str.myString;
return out;
}
String operator += (char *); //Concat
String operator += (String&); //Concats
String operator + (char *); //Concat
String operator + (String&); //Concats
char operator [] (int); //CharAt
//Compare operator overload
bool operator == (char *);
bool operator == (String&);
bool operator != (char *);
bool operator != (String&);
bool operator > (char *);
bool operator > (String&);
bool operator >= (char *);
bool operator >= (String&);
bool operator < (char *);
bool operator < (String&);
bool operator <= (char *);
bool operator <= (String&);
public://Some methods
int Length(); //Return length
bool IsEmpty(); //Is string empty or not
char CharAt(int); //Return char at pos
void ClearString(); //Clear all the string
String SubString(int,int); //Get substring
int Index(char*, int); //Return the first position of a string occurs in *this, if not exists, return -1
int Index(String, int); //Return the first position of a string occurs in *this, if not exists, return -1
String Replace(char*,char*);//Replace substring which is equal to the before
String Replace(String,char*);//Replace substring which is equal to the before
String Replace(char*,String);//Replace substring which is equal to the before
String Replace(String,String);//Replace substring which is equal to the before
String Insert(int, char *); //Insert
String Insert(int, String); //Insert a string before the position
String Delete(int, char *); //Delete
String Delete(int, String); //Delete a string before the position
String Delete(int,int); //Delete a string whose length is specified from pos.
char * MakeChar(); //Get the char array
};
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
String::String() : length(0)
{
myString=new char[1];
Strcpy(myString,"");
}
String::String(char *str)
{
length=Strlen(str);
myString=new char[length+1];
Strcpy(myString,str);
}
String::String(String& str)
{
length=str.length;
myString=new char[length+1];
Strcpy(myString,str.myString);
}
String::~String()
{
delete[] myString;
}
String String::operator =(char *str)
{
if (myString)
delete []myString; //If exist, delete
length=Strlen(str);
myString=new char[length+1];
Strcpy(myString,str);
return *this;
}
String String::operator =(String& str)
{
if (myString)
delete []myString; //If exist, delete
length=str.length;
myString=new char[length+1]; //because '\0''s exist
Strcpy(myString,str.myString);
return *this;
}
String String::operator +=(char *str)
{
length+=Strlen(str);
myString=(char*)realloc(myString, length+1); //Reallocate the size
Strcat(myString,str);
return *this;
}
String String::operator +=(String &str)
{
length+=Strlen(str.myString);
myString=(char*)realloc(myString, length+1); //Reallocate the size
Strcat(myString,str.myString);
return *this;
}
String String::operator +(char *str)
{
length+=Strlen(str);
myString=(char*)realloc(myString, length+1); //Reallocate the size
Strcat(myString,str);
return *this;
}
String String::operator +(String &str)
{
length+=Strlen(str.myString);
myString=(char*)realloc(myString, length+1); //Reallocate the size
Strcat(myString,str.myString);
return *this;
}
inline char String::operator [](int pos)
{
if (pos<0 || pos>length-1)
return 0; //if not exist, return 0
return *(myString+pos);
}
inline bool String::operator == (char *str)
{
return Strcmp(myString,str)==0;
}
inline bool String::operator == (String& str)
{
return Strcmp(myString,str.myString)==0;
}
inline bool String::operator != (char *str)
{
return Strcmp(myString,str)!=0;
}
inline bool String::operator != (String& str)
{
return Strcmp(myString,str.myString)!=0;
}
inline bool String::operator > (char *str)
{
return Strcmp(myString,str)>0;
}
inline bool String::operator > (String& str)
{
return Strcmp(myString,str.myString)>0;
}
inline bool String::operator >= (char *str)
{
return Strcmp(myString,str)>=0;
}
inline bool String::operator >= (String& str)
{
return Strcmp(myString,str.myString)>=0;
}
inline bool String::operator < (char *str)
{
return Strcmp(myString,str)<0;
}
inline bool String::operator < (String& str)
{
return Strcmp(myString,str.myString)<0;
}
inline bool String::operator <= (char *str)
{
return Strcmp(myString,str)<=0;
}
inline bool String::operator <= (String& str)
{
return Strcmp(myString,str.myString)<=0;
}
inline int String::Length()
{
return length;
}
inline bool String::IsEmpty()
{
return length==0;
}
inline char String::CharAt(int pos) //Start from 0
{
if (pos<0 || pos>length-1)
return 0; //if not exist, return 0
return *(myString+pos);
}
inline void String::ClearString()
{
*this="";
}
String String::SubString(int pos, int len) //pos start from 0
{
if (pos<0 || pos>length-1 )
return *this; //if not exist, return this
if (len>length-pos)
len=length-pos; //End to the tail
char *tmpChar=new char[len];
for (int i=0;i<len;i++)
{
*(tmpChar+i)=*(myString+pos+i);
}
*(tmpChar+i)=0;
String tmpString=tmpChar;
return tmpString;
}
int String::Index(char *str, int pos)
{
if (pos<0 || pos>length-1)
return -1; //bad value
int strlen=Strlen(str);
for (int i=pos;i<length-strlen+1;i++)
{
if (SubString(i,strlen) == str)
return i;
}
return -1;
}
int String::Index(String str, int pos)
{
if (pos<0 || pos>length-1)
return -1; //bad value
int Strlen=str.Length();
for (int i=pos;i<length-Strlen+1;i++)
{
if (SubString(i,Strlen) == str)
return i;
}
return -1;
}
String String::Replace(char *strT, char *strV)
{
if (Strcmp(strT,"")==0)
return *this;
int i=0,j=0;
String tmpString="";
while (i!=-1)
{
i=Index(strT,j);//If find it
if (i==-1) //if not find it
{
if (j!=length)
tmpString+=SubString(j,length-j);
break;
}
tmpString+=SubString(j,i-j); //i-j means length
tmpString+=strV;
j+=i+Strlen(strT);
if (j>length)
j=length;
}
if (j==0)
return *this;
return tmpString;
}
String String::Replace(String strT, char *strV)
{
if (strT=="")
return *this;
int i=0,j=0;
String tmpString="";
while (i!=-1)
{
i=Index(strT,j);//If find it
if (i==-1) //if not find it
{
if (j!=length)
tmpString+=SubString(j,length-j);
break;
}
tmpString+=SubString(j,i-j); //i-j means length
tmpString+=strV;
j+=i+strT.Length();
if (j>length)
j=length;
}
if (j==0)
return *this;
return tmpString;
}
String String::Replace(char *strT, String strV)
{
if (Strcmp(strT,"")==0)
return *this;
int i=0,j=0;
String tmpString="";
while (i!=-1)
{
i=Index(strT,j);//If find it
if (i==-1) //if not find it
{
if (j!=length)
tmpString+=SubString(j,length-j);
break;
}
tmpString+=SubString(j,i-j); //i-j means length
tmpString+=strV;
j+=i+Strlen(strT);
if (j>length)
j=length;
}
if (j==0)
return *this;
return tmpString;
}
String String::Replace(String strT, String strV)
{
if (strT=="")
return *this;
int i=0,j=0;
String tmpString="";
while (i!=-1)
{
i=Index(strT,j);//If find it
if (i==-1) //if not find it
{
if (j!=length)
tmpString+=SubString(j,length-j);
break;
}
tmpString+=SubString(j,i-j); //i-j means length
tmpString+=strV;
j+=i+strT.Length();
if (j>length)
j=length;
}
if (j==0)
return *this;
return tmpString;
}
String String::Insert(int pos, char *str)
{
if (pos<0 || pos>length-1)
return *this;
String tmpString="";
tmpString+=SubString(0,pos);
tmpString+=str;
tmpString+=SubString(pos,length-pos);
return tmpString;
}
String String::Insert(int pos, String str)
{
if (pos<0 || pos>length-1)
return *this;
String tmpString="";
tmpString+=SubString(0,pos);
tmpString+=str;
tmpString+=SubString(pos,length-pos);
return tmpString;
}
String String::Delete(int pos, char *str)
{
if (pos<0 || pos>length-1 || Index(str,0)==-1) //pos is invalid, or str is not exist in *this
return *this;
String tmpString="";
tmpString+=SubString(0,pos);
tmpString+=SubString(pos,Index(str,0)-pos);
tmpString+=SubString(Index(str,0)+Strlen(str),length);
return tmpString;
}
String String::Delete(int pos, String str)
{
if (pos<0 || pos>length-1 || Index(str,0)==-1) //pos is invalid, or str is not exist in *this
return *this;
String tmpString="";
tmpString+=SubString(0,pos);
tmpString+=SubString(pos,Index(str,0)-pos);
tmpString+=SubString(Index(str,0)+str.Length(),length);
return tmpString;
}
String String::Delete(int pos, int len)
{
if (pos<0 || pos>length-1 || len==0)
return *this;
if (len>length-pos)
len=length-pos; //At most end to the tail
String tmpString="";
tmpString+=SubString(0,pos);
tmpString+=SubString(pos+len,length); //end to the tail
return tmpString;
}
char *String::MakeChar()
{
char *tmpStr=new char[Length()];
Strcpy(tmpStr,myString);
return tmpStr;
}
#endif // !defined(AFX_STRING_H__B4BE3995_5704_4EA6_890F_85E088401091__INCLUDED_)
/***********************************************************
* Copyright 2008~2010
* Author : Bill S Edison
* Nationality : China
*
* Brief introduction :
* The class is imitated from string class of STL, it have some
* basic functions of a string. + can help string to concatenate,
* and some operators such as '>' "<" '==' and so on can help
* strings for comparing
***********************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -