102-字符串类string更多的方法

关于字符串的更多方法

使用这些方法对原字符串是没有影响的。

1,CompareTo()方法,比较字符串的内容

string s = "siki";//string是System.String的别名,所以小写首字母
int res = s.CompareTo("saki");//两个字符串相等的时返回0。s在字母表中的排序靠前的时返回-1,否则返回1

2,Replace()用另一个字符或者字符串替换字符串中给定的字符或者字符串

string newStr = s.Replace('.', '-');
string newStr = s.Replace(".", "----");//把指定的字符换成指定的字符 或者把指定的字符串换成指定的字符串

3,Split()在出现给定字符的地方,把字符串拆分成一个字符串数组

string[] strArray = s.Split('.');
foreach (var temp in strArray)
{
Console.WriteLine(temp);
}

4,SubString()在字符串中检索给定位置的子字符串

string str = s.Substring(4);
Console.WriteLine(str);

5,ToLower()把字符串转换成小写形式
6,ToUpper()把字符串转换成大写形式
7,Trim()删除首尾的空白

8,Concat()方法,合并字符串
9,CopyTo()方法,把字符串中指定的字符复制到一个数组中
10,Format()方法,格式化字符串

11,IndexOf()方法,取得字符串第一次出现某个给定字符串或者字符的位置

int index = s.IndexOf("devsikid");//我们可以使用这个方法判断当前字符串是否包含一个子字符串,如果不包含,返回-1,如果包含会返回第一个字符的索引

12,IndexOfAny()方法,
13,Insert()把一个字符串实例插入到另一个字符串实例的指定索引处
14,Join()合并字符串数组,创建一个新字符串

转载于:https://www.cnblogs.com/wuxiaohui1983/p/9958791.html

  • 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、付费专栏及课程。

余额充值