字符串类(String 类)详谈

String 类位于 Java lang 包
String s="12345";
String s=new String();
String s=new String("Hello world");

创建对象时 在堆内存创建对象
在占内存中 形成应用
String s = "HelloWorld";
通知双引号直接创建:在字符串常量池中创建字符串
String s2 = s;
String s3 = "HelloWorld";
字符串常量池中的地址
System.out.println(s2 == s3);
String s4 = new String("HelloWorld");
通过new关键字创建 那就跟普通对象创建方式一样
System.out.println(s.equals(s4));

String s5 = "Hello";
String s6 = "World";
String s7 = s5 + s6;
String s8 = "Hello" + "World";
System.out.println(s == s8);

字符串的长度 length()方法
String s=“aaa”;
s.length();

字符串比较的方法 equals()方法
a.equals(b);
b.equals(a);

equals()比较原理
先比较长度
在比较每一位的数字

==和equals()有什么区别
== 基本数据判断值是否一样
引用数据类型判断是否是同一个对象
(根据首地址判断)
equals()
根据里面的值来判断是否为同一个对象

StringBuffer 类 :String 增强版
StringBuffer 声明
StringBuffer 类
StringBuffer 是可以被回收的
sb=null; 出于对内存的考虑建议使用StringBuffer


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现的字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值