C CString 字串拷貝

2021-08-26 08:25:11 字數 3542 閱讀 5299

#ifndef cstring2_hpp

#define cstring2_hpp

// cstring2 優化版本的cstring 增強版本

#include

#include

#include

class cstring2

operator const char* ()

cstring2(const char* p);

cstring2(const cstring2& str);

cstring2();

~cstring2();

cstring2 &operator=(const cstring2& str);

cstring2 &operator=(const char* str);

cstring2 &operator+=(const cstring2& str);

cstring2 operator+(const cstring2& str2);

int find(const char* pszsub,int size = 0);

int find(char ch,int istart = 0 );

//void makelower(char* str);

cstring2 makelower(cstring2 str);

cstring2 &makelower();

cstring2& makeupper();

cstring2& makereverse();

cstring2 mid(int ifirst,int ncount) const;

cstring2 mid(int ifirst) const

//從左邊開始擷取到底幾個

cstring2 left(int ncount) const

//從滴幾個開始擷取

cstring2 right(int ncount) const

char operator(int nindex) const

char &operator(int nindex)

private:

int m_nlenght;

char* m_pdata;

cstring2(int nlen)

};#endif /* cstring2_hpp */

cstring ---- cpp

#include "cstring2.hpp"

cstring2::cstring2(const char* p)

cstring2::cstring2(const cstring2& str)

cstring2::cstring2()

cstring2::~cstring2()

//物件間賦值

cstring2 &cstring2::operator=(const cstring2& str)

strcpy(m_pdata, str.m_pdata);

m_nlenght = nlen;

return *this;

}cstring2 &cstring2::operator=(const char* str)

strcpy(m_pdata,str);

m_nlenght = nlen;

return *this;

}cstring2 &cstring2::operator+=(const cstring2& str)

cstring2 cstring2::operator+(const cstring2& str2)

//根據asii 碼轉換大小寫

cstring2 cstring2::makelower(cstring2 str)else if(str2[i] >= 'a' && str2[i] <= 'z')

}return str2;

}//根據asii 碼轉換大小寫

cstring2 &cstring2::makelower()else if(m_pdata[i] >= 'a' && m_pdata[i] <= 'z')

}return *this;

}//字串反轉

cstring2& cstring2::makereverse()

return *this;

}//字元擷取

cstring2 cstring2::mid(int ifirst,int ncount) const

//從第幾個開始擷取,保留剩下的

//cstring2 cstring2::mid(int ifirst) const

//int find(const char* pszsub,int size = 0)

int cstring2::find(char ch,int istart)

cpp  ---- main

int main(int argc, const char * argv) {

cstring s1 = "abc",s2 = "-xyz";

s1 += s2;

std::cout << s1 << std::endl;

return 0;

int main(int argc, const char * argv) {

cstring s1 = "abc",s2 = "-xyz";

//cstring s = s1+s2;

cstring s = s1;  //深拷貝

cstring s3('*',8);

// s1 = s2;  //直接賦值是屬於淺拷貝

cstring s5;

s5 = s4 = "123456";

if (s1 == s4)

std::cout << "相等" << std::endl;

else

std::cout << "不相等" << std::endl;

if ("123456" != s4)

std::cout << "不等" << std::endl;

else

std::cout << "相等" << std::endl;

int i = 0;

while (i < s.getlength()) {

printf("%c",s[i]);

++i;

std::cout << s1 << std::endl;

std::cout << s2 << std::endl;

std::cout << s << std::endl;

std::cout << s4 << std::endl;

printf("%d \n",s1.getlength());

printf("%d \n",s2.getlength());

std::cout << s.getlength() << std::endl;

std::cout << s3 << std::endl;

std::cout << s4 << "\n" << s5 << std::endl;

//型別轉換字串

const char *p = s1; //c++字串與c語言字串;

return 0;

tieto字串拷貝

c實現記憶體拷貝以及字串拷貝函式 摘 收藏 i nclude i nclude i nclude void memmove kk void dest,const void src,size t count char strcpy kk char strdest,const char strsrc i...

字串拷貝函式

1.strcpy函式 顧名思義字串複製函式 一般函式原型實現方式 該函式的引數是字元指標,也就是可以是字串變數和字元陣列,因為它們的變數名代表首字元位址。字串預設有乙個null結束符,字元陣列沒有。所以此處需要注意 因為src要求有null結束符,所以字元陣列的長度必須大於等於src包含null結束...

snprintf拷貝字串

函式原型 int snprintf char dest,size t n,const char fmt,函式說明 最多從源串中拷貝n 1個字元到目標串中,然後再在後面加乙個0。所以如果目標串的大小為n的話,將不會溢位。函式返回值 若成功則返回存入陣列的字元數,若編碼出錯則返回負值。include i...