C#字符串學習筆記

前言:記得我們老師說過一句話,對字符串的學習程度就是當別人打你一拳你知道痛的情況,所以字符串的處理我們必須學的差不多,這幾篇博客完全是我的學習過程中記錄的筆記,在這里分享一下讓很多剛開始學習.net編程的人能夠很快的學會怎么處理字符串

  1. string的構造方法

(1) String和string意思一樣,只是String是.net FrameWork提供的,而string是C#語言提供的

?(2)字符串是不可變的? ??

 class Program
{static void Main(string[] args){string str = "123456";(*)string s1 = new string(new char[] { '我', '是', '韓', '迎', '龍' }); //輸出字符串(*)string s2 = new string('0', 10);  //輸出10個0Console.WriteLine(s1);Console.WriteLine(s2);Console.WriteLine(5.ToString("00000")); //前面補0string str1 = "韓閃閃";char[] chs = str1.ToCharArray();for (int i = 0; i < chs.Length; i++){if (chs[i] == '閃'){chs[i] = '珊';}}str1 = new string(chs);Console.WriteLine(str1);Console.ReadKey();}}
  1. 字符串的方法和常量

(1) 字符串賦初值的三種方法

???? string str = "";

???? string str1 = string.Empty;

???? string str2 = null;

(2)判斷字符串是否為空

???? Console.WriteLine(string.IsNullOrEmpty(string.Empty));

(3)字符串拘留池(駐留池)

???? 使用字符串的時候,如果字符串相同,那么所有變量都指向同一個地方

(4)字符串的比較方法

???? 1)比較兩個字符串可以使用==或者Equals

???? 2)Equals的使用

???? ?????? ->靜態版本? string.Equals(object objA,object objB[,StringComparison com]);

???? ?????? ->實例版本? str.Equals(object objA);

???? 3)一個靜態的方法 Compare

???? ?????? ->string.Compare(str1,str2)

??????????? ?????? str1>str2?? 1

??????????? ?????? str1=str2?? 0

?????????????????? str1<str2?? -1

(5)全部轉換為小寫,或者大寫? ToLower,ToUpper

???? string str="assdfdSDS";

???? str=str.ToUpper();

???? Console.WriteLine(str); //全部變成大寫輸出

(6)去掉兩端的空格和指定的元素

???? string str=" 1234343 ";

???? str=str.Trim();

???? 1)Trim(params char[] chs)

????????????? string str = "? 11154544dfddf4351111? ";

??? ???str = str.Trim(' ', '1');

????? ?Console.WriteLine(str);

??? ??//輸出結果:54544dfddf435

(7)字符串的合并和分隔

???? 1)合并

???? ?????? string[] str = { "韓迎龍", "李瀟絮", "小李飛刀" };

???? ?????? string s = string.Join("|", str);

???? ?????? Console.WriteLine(s);

???? ?????? Console.ReadKey();

???? 2)分隔

???? ?????? 語法:

???? ????????????? string[] <string>.Split(params char[] chs)

??????????? ?????? string[] <string>.Split(new char[] chs,StringSplitOptions.RemoveEmpty)

?????????????????? string s = "韓迎龍|23|男|女";

?????????????????? string[] str = s.Split('|');

?????????????????? Console.ReadKey();

(8)判斷字符串的長度

???? 1)語法:<string>.Length;

???? 2)字符串可以像數組一樣使用"下標"訪問(索引)

??????????? for(int i=0;i<str.Length;i++)

??????????? {

?????????????????? Console.WriteLine(str[i]);

??????????? }

(9)字符串查找

???? 1)判斷是否包含 Contains

??????????? ->語法:bool <string>.Contains(string str);

string str = "張三,韓迎龍,李四,王五,馬六,楊七";if (str.Contains("韓迎龍")){Console.WriteLine("查找到了");}else{Console.WriteLine("沒找到");}

??????????? ->模擬Contains的實現(很局限)

static bool MyContains(string src, char cmp){bool isTrue = false;for (int i = 0; i < src.Length; i++){if (src[i] == cmp){isTrue = true;break;}}return isTrue;}

???? 2)IndexOf 尋找索引

??????????? ->語法:

?????????????????? int <string>.IndexOf(char ch);

?????????????????? int <string>.IndexOf(char ch[,int startIndex]);

?????????????????? int <string>.IndexOf(string str[,int startIndex]);

????????????????????????? string str = " 好好學習,天天向上";

????????????????????????? int indeof = str.IndexOf('向');

??????????? ->模擬MyIndex的算法(很局限)? ? ? ? ? ? ? ? ? ?

static int MyIndex(string src, char cmp){int index = -1;for (int i = 0; i < src.Length; i++){if (src[i] == cmp){index = i;break;}}return index;}

???? 3)找出字符串中所有的"e"??

static void Main(string[] args){string str = "asfseeefsafserefeefsde";int index = -1;do{index = str.IndexOf('e', index + 1);if (index != -1){Console.WriteLine(index);}} while (index != -1);}    

(10)LastIndexOf 從右邊往左邊找

???? ?string str = "32errrwefsfsd";

??? int index = str.LastIndexOf('e'); //輸出7

(11)子字符串

???? 1)語法:string <string>.SubString(開始的索引,字符串的長度);

???? ????????????? string <string>.SubString(開始的索引);

???? 2)案例:

???? ?????? string str = @"F:\heima\practice\string.exe";

?? ????int start = str.IndexOf('\\');

????? ?int length = str.IndexOf('\\', start + 1) - start;

?????? string s = str.Substring(start + 1, length - 1);

???? 3)跑馬燈的效果? ? ? ? ?

string str = "北京歡迎您  ";while (true){Console.Clear();str = str.Substring(1) + str.Substring(0, 1);Console.WriteLine(str);System.Threading.Thread.Sleep(200);}

字符串的插入,移除,替換

????????????? 1)insert

???????????????????? string str = "時間就是一把殺豬刀啊";

??????????? str = str.Insert(0, "世界上");

????????????? 2)remove

???????????????????? string str = "時間就是一把殺豬刀啊";

??????????? str = str.Remove(0, 2);

????????????? 3)replace

???????????????????? string str = "時間就是一把殺豬刀啊";

??????????? str = str.Replace("時間", "小賢");

?????? (13)判斷結束和開始

????????????? 1)判斷字符<string>串是否已某字符串開始或結束

????????????? 2)bool <string>.StartsWith(string str);

????????????? 3)bool <string>.EndsWith(string str);? ? ? ? ? ? ?

1)第一個案例string[] str = {"白天不懂夜的黑.mp3","夢想的力量.mp3","舊夢重彈.wma","我相信.mp3" };for (int i = 0; i < str.Length; i++){if (str[i].EndsWith(".mp3")){Console.WriteLine(str[i] + "下載成功");}else{Console.WriteLine(str[i] + "格式不正確");}}2)第二個案例static void Main(string[] args){string path1= "http://www.cnblogs.com";string path2 = "http://www.cnblogs.com/";string file = "舊夢重彈.mp3";string downfile = GetFileName(path1, file);Console.WriteLine(downfile);Console.ReadKey();}static string GetFileName(string path, string file){if (path.EndsWith("/")){return path + file;}else{return path + '/' + file;}}

(14)字符串的幾個練習題

  ?1)接收用戶輸入的字符串,將其中的字符以相反的順序輸出"韓迎龍"->"龍迎韓"

static void Main(string[] args){string str = "韓迎龍";//第一種寫法for (int i = str.Length-1; i  >=0; i--){Console.Write(str[i]  );}//第二種寫法string strname = Exercise(str);Console.WriteLine(strname);}static string Exercise(string str){char[] chs = str.ToCharArray();  //變成char數組for (int i = 0; i < chs.Length / 2; i++){char temp = chs[i];chs[i] = chs[chs.Length - i - 1];chs[chs.Length - i - 1] = temp;}return new string(chs);}

?  2)接受用戶輸入的一句英文,將其中的單詞反序輸出。"I Love you"->"I evol uoy"

static void Main(string[] args){string str = "I Love You";str = Exercise(str);Console.WriteLine(str);}static string Exercise(string str){string[] strs = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);for (int i = 0; i < strs.Length; i++){strs[i] = Exercisetwo(strs[i]);}return string.Join(" ", strs);}static string Exercisetwo(string str){char[] chs = str.ToCharArray();  //變成char數組for (int i = 0; i < chs.Length / 2; i++){char temp = chs[i];chs[i] = chs[chs.Length - i - 1];chs[chs.Length - i - 1] = temp;}return new string(chs);}

?   3)"2012年12月21日"從日期字符串中把年月日分別取出來,打印到控制臺? ? ?

static void Main(string[] args){string str = "2012年10月03日";string[] strs = str.Split(new char[] { '年', '月', '日' });Console.WriteLine(string.Join(" ", strs));Console.ReadKey();}

?  4)把csv文件中的聯系人的姓名和電話顯示出來,簡單模擬

? ?  注:首先將一個name.txt結構的文檔放在項目的Bin的Debug目錄下面,使其代碼能夠讀到這個,里面的結構是"韓迎龍|男|23"

static void Main(string[] args){string[] lines = File.ReadAllLines("name.txt");for (int i = 0; i < lines.Length; i++){string[] strTemp = lines[i].Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);Console.WriteLine("姓名:{0},年齡:{1}", strTemp[0], strTemp[2]);}}

轉載于:https://www.cnblogs.com/hanyinglong/archive/2012/10/05/2711971.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274578.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274578.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274578.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Git基礎教程(必學)

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

用戶體驗改善案例_優化用戶體驗案例研究的五種方法

用戶體驗改善案例重點 (Top highlight)I’ve had the opportunity to give several portfolio reviews, and I want to share some common themes I see and how you can improve them to put your best foot forward as you search for that new product design gig.我有機會發…

video from html5

掌握HTML5中的多媒體--視頻(video) 除非你一直生活在一個偏遠的島嶼上,過去一年左右的時間,你應該已經聽說過HTML5的各式炒作。HTML5將重塑富Web應用的未來。 下面 Figure 1的示例展示了HTML5中video標簽與傳統的object標簽的不同. Figure 1 1. <section> 2. <h…

我撿到寶了!2022版前端面試上岸手冊,最新最細致!

大裁員背景下&#xff0c;沒什么比辭職后找不到工作更扎心&#xff01;在行情好轉前&#xff0c;前端程序員只能“猥瑣發育”&#xff0c;不輕易跳槽&#xff0c;同時要修煉內功&#xff1a;對八股文、底層源碼、重點項目等進行查缺補漏&#xff0c;靜待行情好轉抓住機會&#…

flo file_Flo菜單簡介:可擴展的拇指友好型移動導航

flo fileWhen it comes to using my phone, I’m a thumb guy and I like using my phone held in one hand. Well, apparently 49% of us prefer it like this.說到使用手機&#xff0c;我是個拇指小伙&#xff0c;我喜歡用一只手握住手機。 好吧&#xff0c;顯然我們當中有49…

超炫的iphone應用UI/UX設計賞析

日期&#xff1a;2012-10-5 來源&#xff1a;GBin1.com 要想成為一款成功的iOS應用&#xff0c;不單單是功能設計&#xff0c;還需要有超棒的用戶界面和用戶體驗的完美設計。為了帶給大家更多的設計靈感&#xff0c;今天我們分享另外一套來自dribbble的iOS應用UI和UX設計&…

Git實戰進階教程

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

什么是設計模式_什么是設計?

什么是設計模式Imagine, you are out waiting for a taxi. You are about to miss your appointment. You wait for minutes but Good Lord! — there’s not a single taxi that can offer you a ride.想象一下&#xff0c;您正在外面等出租車。 您將錯過約會。 您等待幾分鐘&…

hive實現not in

當前HIVE 不支持 not in 中包含查詢子句的語法&#xff0c;形如如下的HQ語句是不被支持的: 查詢在key字段在a表中&#xff0c;但不在b表中的數據 select a.key from a where key not in(select key from b) 該語句在hive中不支持 可以通過left outer join進行查詢,&#xff0…

有哪些值得學習的大型 React 開源項目?

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

成年人的樣子是什么樣子_不只是看樣子

成年人的樣子是什么樣子As a branding, packaging, and digital product designer, both at Input Logic and as a freelancer, I work with clients across a wide array of industries, and am responsible for simultaneously getting to the heart of what each client wan…

HDU 3664 Permutation Counting(DP)

題目鏈接 弱爆啦&#xff0c;組合弱爆了&#xff0c;反正是沒想出來怎么搞這個題&#xff0c;其實這個公式不難推啊&#xff0c;反正就是沒推出來。今天隊內賽&#xff0c;實在是沒辦法了&#xff0c;暴力寫了個DFS&#xff0c;先把10以內的打出表來&#xff0c;發現類似楊輝三…

如何在工作中打造影響力,帶動同事?

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

谷歌maps菜單語言設置_Google Maps:拯救未來之路— UX案例研究

谷歌maps菜單語言設置I have a lousy sense of direction, so Google Maps has always been my right-hand app. On a whim last year, I decided to skip the beach and sunburn and head to Budapest for spring break. That’s when Google Maps became my best friend.我的…

this和prototype

this出現在構造函數中&#xff0c;更多的是表示一種特有的屬性&#xff1b; prototype主要用于拓展函數的屬性&#xff0c;方法。 在函數類實例化的時候&#xff0c;this的屬性需要復制相應的副本&#xff0c;prototype不用。 function Blog(title,content) { this.titletitle;…

1萬小時后,我從外包走進了字節跳動,現在出了一本書,文末送書!

謹以此書獻給相信“努力有用”的你by 大史不說話《 前端跨界開發指南&#xff1a;JavaScript工具庫原理解析與實戰》先做個自我介紹我是大史不說話&#xff0c;是一名前端工程師&#xff0c;一個相信“努力有用”的、不太聰明的、行動力還可以的程序員。曾經因為一篇《10000小時…

視覺設計師跟平面設計_使設計具有視覺吸引力

視覺設計師跟平面設計Interaction Design is very gratifying.交互設計非常令人滿意。 From fast critical thinking to extracting ideas in tangible forms within the team is sure fun and challenging.從快速的批判性思維到在團隊內部以有形的形式提煉想法&#xff0c;無…

ExtJs4 筆記 Ext.tab.Panel 選項卡

本篇講解選項卡控件。 一、基本選項卡 首先我們來定義一個基本的選項卡控件&#xff0c;其中每個Tab各有不同&#xff0c;Tab的正文內容可以有三種方式獲取&#xff1a; 1.基本方式:通過定義html和items的方式。 2.讀取其他html的信息:通過設置contentEl就可以獲取其他html的信…

一直刷不動算法題,懷疑人生?試試五毒掌法!

大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。歷史面試系列。另外…

還在用開發者工具上傳小程序? 快來試試 miniprogram-ci 提效摸魚

1. 前言大家好&#xff0c;我是若川。持續組織了近一年的源碼共讀活動&#xff0c;感興趣的可以 加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含包含jQuery、underscore、lo…