按照python的字符串处理,基于utf8编码写的字符串类,方便操作,和 我写的另一个基础类ulist类,联合起来,就是完成的基础类。
ulist.h 在我第第二篇文章里,可以自行下载。
有需要解答的可以找我。
可以互相转化。
类成员如下:
测试图片:
class ustring{
public:
string str;
ustring();
long len();
// bool find(string put_st,long start_number,long end_number);
// long index(string index_str,long start_number,long end_number);
long count(string count_str);
ustring replace(string replace_str,string end_str);
string captitalize();// 每个单词的首字母大写,其他小写。
string lower();// 全部小写
string upper();// 全部大写
// // 判断专区
bool in(string in_str);// 返回-1 或者位置.
bool isnumber();
double number();
bool isstr();
ulist split(string split_str);// 以指定字符切割,返回内容。
// // 重载区域
ustring operator+(ustring add_ustring);
// // ustring operator=(string set_string);
string operator[](long mc);
string operator()(long start_number,long end_number);// 截取错误
bool operator==(ustring put_str);
bool operator!=(ustring put_str);
};
测试文档
```cpp
#include "usring.h"
int main()
{ system("chcp 65001");
ulist s1;
s1.append("Dd");
cout<<s1<<endl;
ustring s2;
s2.str="dd长度";
cout<<"字符串长度"<<s2.len()<<endl;
ustring s3;
s3.str="123再4陈,永远dxd为";
cout<<s3[-1]<<endl;
cout<<s3[-2]<<endl;
cout<<s3[0]<<endl;
cout<<s3[1]<<endl;
cout<<"在里面"<<endl;
cout<<s3(0,3)<<endl;
cout<<"全大写 "<<s2.upper()<<endl;
cout<<"全小写 "<<s2.lower()<<endl;
printf("\n");
ustring st2;
st2.str="abcdddddfwef的";
ustring add= st2.replace("的","测试插入");
cout<<add<<endl;
// cout<<s2<<endl;
// cout<<st2<<endl;
// cout<<st2<<endl;
cout<<"查找其中的元素个数 "<<st2.count("a")<<endl;
cout<<add.split("d")<<endl;
ulist mylist=add.split();
for(node *pp=mylist.head->next;pp!=mylist.head;pp=pp->next){
cout<<"测试:"<<pp->str<<endl;
}
ustring ace;
ace.str="dd";
ustring acw;
acw.str="dd";
if(acw!=ace){
cout<<"相等"<<endl;
}else{
cout<<"不相等"<<endl;
}
if(ace.in("")){
cout<<"在里面"<<endl;
}else{
cout<<"不在里面"<<endl;
}
if(ace.isstr()){
cout<<"文字"<<endl;
}else{
cout<<"数字"<<endl;
}
if(ace.isnumber()){
cout<<"是文字"<<endl;
}else{
cout<<"是数字"<<endl;
}
system("pause");
return 0;
}
头文件
```cpp
#include "ulist.h"
// 需要C++11标准及以上。
# include <iostream>
#include <windows.h>
#include <string>
using namespace std;
/*
作者: 广都
qq: 1975767630
微信: wo15985300747
还有一个配套的ustring类 请查看我的csdn
广都--编程每日问 qq_49758204
*/
class ustring{
public:
string str;
ustring();
long len();
// bool find(string put_st,long start_number,long end_number);
// long index(string index_str,long start_number,long end_number);
long count(string count_str);
ustring replace(string replace_str,string end_str);
string captitalize();// 每个单词的首字母大写,其他小写。
string lower();// 全部小写
string upper();// 全部大写
// // 判断专区
bool in(string in_str);// 返回-1 或者位置.
bool isnumber();
double number();
bool isstr();
ulist split(string split_str);// 以指定字符切割,返回内容。
// // 重载区域
ustring operator+(ustring add_ustring);
// // ustring operator=(string set_string);
string operator[](long mc);
string operator()(long start_number,long end_number);// 截取错误
bool operator==(ustring put_str);
bool operator!=(ustring put_str);
};
ostream& operator<<(ostream& os, const ustring& print_str){
cout<<print_str.str;
return os;
}
string ustring::operator()(long start_number,long end_number){ // error : not sub string
long len=this->len();
long s1=0,s2=len;
if(start_number <0){
s1= len+end_number;
}
else{
s1=start_number;
}
if(end_number<0){
s2=len+end_number;
}
else{
s2=end_number;
}
if(s1>=s2){
long s3=s1;
s1=s2;
s2=s3;
}
long len_number=0;
int num = this->str.size();
int i = 0;
while(i < num){
int size = 1;
if(this->str[i] & 0x80){
char temp = this->str[i];
temp <<= 1;
do{
temp <<= 1;
++size;
}while(temp & 0x80);
}
if (start_number==len_number){
s1=i;
}
if(end_number==len_number){
s2=i;
// break;
}
i += size;
len_number =len_number+1;
}
string subWord;
subWord = this->str.substr(s1, s2);
return subWord;
}
string ustring::operator[](long mc){
long len_number=0;
int num = this->str.size();
if(mc<0){
mc=mc+this->len();
}
cout<<"number "<<mc<<endl;
int i = 0;
while(i < num){
int size = 1;
if(this->str[i] & 0x80){
char temp = this->str[i];
temp <<= 1;
do{
temp <<= 1;
++size;
}while(temp & 0x80);
}
if (len_number==mc){
string subWord;
subWord = this->str.substr(i, size);
return subWord;
}
i += size;
len_number =len_number+1;
}
return "";
}
ustring ustring::operator+(ustring add_ustring){
this->str= this->str+add_ustring.str;
return *this;
}
bool ustring::operator==(ustring put_str){
if(this->str ==put_str.str){
return true;
}
else{
return false;
}
}
bool ustring::operator!=(ustring put_str){
if(this->str ==put_str.str){
return false;
}
else{
return true;
}
}
// ustring& ustring::operator=(const ustring & set_string){
// this->str=set_string.str;
// return *this;
// }
// Apple& operator= (const Apple & tem) {
// this->SetA(fun.GetA());
// return *this;
// }
ustring::ustring(){
// string *new_str=new string;
this->str= "";
}
long ustring::len(){
long len_number=0;
int num = this->str.size();
int i = 0;
while(i < num){
int size = 1;
if(this->str[i] & 0x80){
char temp = this->str[i];
temp <<= 1;
do{
temp <<= 1;
++size;
}while(temp & 0x80);
}
len_number=len_number+1;
i += size;
}
return len_number;
}
#include <iostream>
#include <algorithm> //必需
string ustring::lower(){
string str=this->str;
transform(str.begin(),str.end(),str.begin(),::tolower);
return str;
}
string ustring::upper(){
string str=this->str;
transform(str.begin(),str.end(),str.begin(),::toupper);
return str;
}
ustring ustring::replace(string replace_str,string end_str){
ustring add;
string::size_type pos=0;
string::size_type a=replace_str.size();
string::size_type b=end_str.size();
// int mi=0;
while((pos=this->str.find(replace_str,pos))!=string::npos)
{
this->str.replace(pos,a,end_str);
pos+=b;
// mi=mi+1;
}
// return mi;
add.str=this->str;
// return *this;
return add;
// return this->str;
}
long ustring::count(string count_str){
ustring add;
string::size_type pos=0;
string::size_type a=count_str.size();
int mi=0;
while((pos=this->str.find(count_str,pos))!=string::npos){
pos+=a;
mi=mi+1;
}
return mi;
}
// bool ustring::find(string put_st,long start_number,long end_number){
// ustring add;
// string::size_type pos=0;
// string::size_type a=count_str.size();
// int mi=0;
// while((pos=this->str.find(count_str,pos))!=string::npos){
// pos+=a;
// if (m)
// mi=mi+1;
// }
// return mi;
// }
// #include <iostream>
// #include <string>
// #include <vector>
//字符串分割函数
ulist ustring::split(string split_str=""){
ulist re_list;
if(split_str!=""){
std::string::size_type pos;
str += split_str;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++){
pos = str.find(split_str, i);
if (pos < size){
std::string s = str.substr(i, pos - i);
if(s!=""){
re_list.append(s);
}
i = pos + split_str.size() - 1;
}
}
}else{
int num = this->str.size();
int i = 0;
while(i < num){
int size = 1;
if(this->str[i] & 0x80){
char temp = this->str[i];
temp <<= 1;
do{
temp <<= 1;
++size;
}while(temp & 0x80);
}
string subWord;
subWord = this->str.substr(i, size);
// cout<<subWord<<endl;
re_list.append(subWord);
i += size;
}
}
return re_list;
}
bool ustring::in(string in_str){
ustring add;
string::size_type pos=0;
string::size_type a=in_str.size();
while((pos=this->str.find(in_str,pos))!=string::npos){
pos+=a;
return true;
}
return false;
}
double ustring::number(){
return to_num(this->str);
}
//判断字符串是否为整数(仅判断格式,不考虑范围)
bool isInt(const char* str){
bool isNum = false;
int index = 0;
for (; *str != '\0'; str++, index++){
switch (*str){
case '0':case'1':case'2':case'3':case'4':case'5':
case'6':case'7':case'8':case'9':
isNum = true;
break;
case '-':case '+':
if (index != 0){
return false;
}
break;
default:
return false;
}
}
if (!isNum){
return false;
}
return true;
}
//判断字符串是否为浮点数(仅判断格式,不考虑范围)
bool isFloat(const char * str){
bool isE = false,
isPoint = false,
numBefore = false,
numBehind = false;
int index = 0;
for (; *str != '\0'; str++, index++){
switch (*str){
case '0':case'1':case'2':case'3':case'4':case'5':
case'6':case'7':case'8':case'9':
if (isE){
numBehind = true;
}
else{
numBefore = true;
}
break;
case '+':case '-':
if (index != 0){
return false;
}
break;
case 'e':case 'E':
if (isE || !numBefore)
{
return false;
}else{
isPoint = true;
index = -1;
isE = true;
}
break;
case '.':
if (isPoint){
return false;
}
else{
isPoint = true;
}
break;
default:
return false;
}
}
if (!numBefore){
return false;
}
else if (isE && !numBehind){
return false;
}
return true;
}
bool ustring::isnumber(){
if(isInt(this->str.c_str()) or isFloat(this->str.c_str())){
return true;
}
else{
return false;
}
}
bool ustring::isstr(){
if(isInt(this->str.c_str()) or isFloat(this->str.c_str())){
return false;
}
else{
return true;
}
}