C#字串

來源:互聯網
上載者:User

標籤:style   blog   http   ar   io   color   使用   sp   for   

///C#字串是使用string關鍵字聲明的一個字元數組,它也是一個對象,封裝了所有字串操作,如比較、插入、刪除、尋找等。
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { //建立一個字串 string str1 = "apple orange banana"; Console.WriteLine("str1:+str1"); ///建立另一個字串 string str2 = str1 + "peach"; Console.WriteLine("str2:" + str2); //比較兩個字串是否一致 if (String.Compare(str1, str2) == 0) { Console.WriteLine("str1 和str2兩個字串一致"); } else { Console.WriteLine("str1和str2兩個字串不一致"); } //從第0個位元組開始尋找空格的位置 int n=str1.IndexOf(‘ ‘,0); Console.WriteLine("str1第一個空格在第{0}個位元組",n); ///刪除第1個空格之後的所有字元 str2=str1.Remove(n); Console.WriteLine("刪除str1第一個空格後的所有字元:"+str2); ///將所有空格替換為- str2=str2.Replace(‘ ‘,‘-‘); Console.WriteLine("將所有的空格替換為-:"+str2); ///在第一個空格之後插入@@@ str2=str1.Insert(n,"peach"); Console.WriteLine("在str1第一個空格插入peach:"+str2); ///取第一個空格後的6個字元 str2=str1.Substring(n+1,6); Console.WriteLine("取str1第一個空格後的6個字元:"+str2); ///以空格為標記,將字串分解為若干個新的字串 char[]chars={‘ ‘}; string[]strs=str1.Split(chars); Console.WriteLine("以空格為標識符,將str1分解為:"); for(int i=0;i<strs.Length;i++) Console.WriteLine(i+":"+strs[i]); ///按任意鍵退出 Console.ReadKey(); } }}

C#字串

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.