- 字符串类的常用成员函数
- 重载数组访问操作符 [ ]
char& operator [] (int i);
char operator [] (int i) const;
注意事项:当 i 的取值不合法时,抛出异常,合法的范围是 (0 <= i) && (i < m_length)
char& operator [](int i);
char operator [](int i)const;
char& String::operator [](int i)
{
if(i >= 0 && i < m_length)
{
return m_str[i];
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid");
}
}
char String::operator [](int i)const
{
return (const_cast<String&>(*this))[i];
}
#include <iostream>
#include "XiebsString.h"
using namespace std;
using namespace XiebsLib;
int main()
{
String s = "xiebs";
for(int i = 0; i < s.length(); i++)
{
cout << s[i] << endl;
}
return 0;
}
- 判断是否以指定字符串开始或结束
bool equal(const char* l, const char* r, int len)const;
bool startWith(const char* s) const;
bool startWith(const String& s) const;
bool endOf(const char* s) const;
bool endOf(const String& s) const;
bool String::equal(const char* l, const char* r, int len)const
{
bool ret = true;
for(int i = 0; i < len && ret; i++)
{
ret = ret && (l[i] == r[i]);
}
return ret;
}
bool String::startWith(const char* s)const
{
bool ret = (s != nullptr);
if(ret)
{
int len = strlen(s);
ret = (len < m_length) && equal(m_str, s, len);
}
return ret;
}
bool String::startWith(const String& s)const
{
return startWith(s.m_str);
}
bool String::endOf(const char* s)const
{
bool ret = (s != nullptr);
if(ret)
{
int len = strlen(s);
char* str = m_str + (m_length - len);
ret = (len < m_length) && equal(str, s, len);
}
return ret;
}
bool String::endOf(const String& s)const
{
return endOf(s.m_str);
}
#include <iostream>
#include "XiebsString.h"
using namespace std;
using namespace XiebsLib;
int main()
{
String s = "hello world";
cout << s.startWith("he") << endl;
cout << s.endOf("world") << endl;
return 0;
}
- 在指定位置处插入字符串
String& insert(int i, const char* s);
String& insert(int i, const String& s);
String& insert(int i, const char* s);
String& insert(int i, const String& s);
String& String::insert(int i, const char* s)
{
if(i >= 0 && i <= m_length)
{
if(s != nullptr && s[0] != '\0')
{
int len = strlen(s);
char* str = reinterpret_cast<char*>(malloc(m_length + len + 1));
if(str)
{
strncpy(str, m_str, i);
strncpy(str + i, s, len);
strncpy(str + i + len, m_str + i, m_length - i);
str[m_length + len] = '\0';
free(m_str);
m_str = str;
m_length = m_length + len;
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "no memory to assign new string value...");
}
}
}
else
{
THROW_EXCEPTION(IndexOutOfBoundsException, "Parameter i is invalid");
}
return *this;
}
String& String::insert(int i, const String& s)
{
return insert(i, s.m_str);
}
#include <iostream>
#include "XiebsString.h"
using namespace std;
using namespace XiebsLib;
int main()
{
String s = "";
s.insert(0, "xiebs");
s.insert(5, " nb");
cout << s.str() << endl;
return 0;
}
- 去掉字符串两端的空白字符:
String& trim();
String& trim();
String& String::trim()
{
int b = 0;
int e = m_length-1;
while (m_str[b] == ' ') b++;
while (m_str[e] == ' ') e--;
if(b == 0)
{
m_str[e + 1] = '\0';
m_length = e + 1;
}
else
{
for(int i=0, j=b; j<=e; i++, j++)
{
m_str[i] = m_str[j];
}
m_str[e - b + 1] = '\0';
m_length = e - b + 1;
}
return *this;
}
#include <iostream>
#include "XiebsString.h"
using namespace std;
using namespace XiebsLib;
int main()
{
String s = " abc ";
cout << s.trim().str() << endl;
if( s.trim().insert(0, "hello,").endOf("abc") && s.startWith("hello") )
{
cout << "[" << s.trim().str() << "]" << endl;
}
return 0;
}