1.串的含义
串(string)是由零个或多个字符组成的有限序列,又名叫字符串。
2.生成一个其值等于字符串常量chars的串T
static string StrAssign(string T_l , const char *chars);
string String::StrAssign(string T_l, const char *chars)
{
T_l = chars;
return T_l;
}
3. 串S存在,由串S复制得串T
static string StrCopy(string &T_l, const string S_l);
string String::StrCopy(string &T_l,const string S_l)
{
if ((&T_l == nullptr) || (&S_l == nullptr))
return 0;
char *T = new char[MAXSIZE];
char *S = new char[MAXSIZE];
for (int i = 0; i < S_l.length(); i++)
S[i] = S_l[i];
while ((*T++ = *S++) != '\0')
;
T_l = T;
return T_l;
}
4.若串S存在,将串清空
static string ClearString(string &S);
string String::ClearString(string &S)
{
if (S.length() != 0)
S = string();
return S;
}
5.若串S为空,返回true,否则返回false
static bool StringEmpty(const string &S);
bool String::StringEmpty(const string &S)
{
if (S.length() != 0)
return false;
return true;
}
6.返回串S的元素个数
static int StringLength(const string &S);
int String::StringLength(const string &S)
{
int i = 0;
while (S[i] != '\0')
i++;
return i;
}
7.判断两个字符串大小
static int StrCompare(const string &T, const string &S);
int String::StrCompare(const string &T, const string &S)
{
int i = 0;
while (T[i] && (T[i] == S[i]))
++i;
return T[i] - S[i];//若T>S,返回正数,若T<S,返回负数,若T=S,返回0
}
8.截取字符串
static string SubString(const string &S, int Pos, int len);
string String::SubString(const string &S, int Pos, int len)
{
if (Pos < 0) { //Pos小于0时抛出异常
throw new StringIndexOutOfBoundsException(Pos);
}
if (Pos + len > value.length) { //len大于本字符串长度时抛出异常
throw new StringIndexOutOfBoundsException(Pos+len);
}
if (len < 0) { //截取长度小于0时抛出异常
throw new StringIndexOutOfBoundsException(len);
}
return ((Pos == 0) && (Pos+len == S.length())) ? *this //当开始位置等与0且结束位置等于字符串长度时返回本身,否则新建一个String返回
: new String(S, Pos, len);
}
9.返回相同子串的第一个字符下标
static int Index(string S, string T, int Pos);
int String::Index(string S, string T, int Pos)
{
int m, n,i;
string Sub;
if (Pos > 0)
{
n = S.length();
m = T.length();
i = pos;
while (i <= n - m + 1)
{
Sub = SubString(S, i, m);
if (StrCompate(Sub, T) != 0)
++i; //如果子串不相等,继续生成新的子串
else
return i; //如果子串和字符串T相等,则返回下标
}
}
return 0;
}
10.替换子串内容
static bool Replace(string &S, const string &T, const string &V);
int length1 = S.length();
int length2 = T.length();
bool String::Replace(string &S, const string &T, const string &V)
{
flag = 0;
int index = Index(S, T, 0);
if (index) == 0 && flag == 0)
{
throw new exception("没有相同子串");
}
else
return true;
flag++;
string S1 = SubString(S, 0, index - 1);
string S2 = SubString(S, index + length2, length1 - lengh2 - S1.length());
S = Concat(S1, V);
S = Concat(S, S2);
Replace(S, T, V);
}
11.插入字符串
static bool StrInsert(string &S, const int Pos, const string &T);
/*串S和T存在,1≤pos≤StrLength(S)+1.在串S的第Pos个字符之前插入串T*/
bool String::StrInsert(string &S, int Pos, const string &T)
{
if ((&S == nullptr) || (&T == nullptr))
throw new exception("输入错误");
String S1 = SubString(S, 0, Pos - 1);
String S2 = SubString(S, Pos, S.length() - Pos + 1);
S = Concat(S1, T);
S = Concat(S, S2);
return true;
}
12.删除字符串
static bool StrDelete(string &S, const int Pos, const int len);
bool String::StrDelete(string &S, const int Pos, const int len)
{
if (&S == nullptr)
throw new exception("输入错误");
string S1 = SubString(S, 0, Pos - 1);
string S2 = SubString(S, Pos + len + 1, S.length() - Pos - len);
S = Concat(S1, S2);
return true;
}