1,字符串类中的新功能(本文代码已集成到字符串类——字符串类的创建(上)中,这里讲述函数实现原理):
2,子串查找(KMP 算法直接运用):
1,int indexOf(const char* s) const;
2,int indexOf(const String& s) const;
3,子串查找成员函数的声明:
int indexOf(const char* ) const;
int indexOf(const String& s) const;
4,子串查找成员函数的定义:
1 int String::indexOf(const char* s) const // 子串查找,返回下标 2 { 3 return kmp(m_str, s ? s : ""); 4 } 5 6 int String::indexOf(const String &s) const 7 { 8 return kmp(m_str, s.m_str); 9 }
3,在字符串中将指定的子串删除:
1,String& remove(const char* s);
2,String& remove(const String& s);
1,根据 kmp 在目标字符串中查找子串的位置;
2,通过子串位置和子串长度进行删除;
3,删除指定字符串成员函数的声明:
String& remove(int i, int len);
String& remove(const char* s);
String& remove(const String& s);
4,删除指定字符串成员函数的定义:
1 /* 删除下标 i 处长度为 len 的字符串 */ 2 String& String::remove(int i, int len) // 和 insert() 返回的是相同的函数,还可以以字符串类继续访问,如查看删除后的字符串等 3 { 4 if( (0 <= i ) && (i < m_length) ) 5 { 6 int n = i; 7 int m = i + len; // 在 (n, m) 范围之内的字符都要删除掉 8 9 while( (n < m) && (m < m_length) ) // 删除的字符串长度是不能大于当前的长度的,否则没有意义 10 { 11 m_str[n++] = m_str[m++]; // 这个赋值很经典 12 } 13 14 m_str[n] = '