C#字符串学习笔记
前言:记得我们老师说过一句话,对字符串的学习程度就是当别人打你一拳你知道痛的情况,所以字符串的处理我们必须学的差不多,这几篇博客完全是我的学习过程中记录的笔记,在这里分享一下让很多刚开始学习.net编程的人能够很快的学会怎么处理字符串
- string的构造方法
(1) String和string意思一样,只是String是.net FrameWork提供的,而string是C#语言提供的
(2)字符串是不可变的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | class Program { static void Main( string [] args) { string str = "123456" ; (*) string s1 = new string ( new char [] { '我' , '是' , '韩' , '迎' , '龙' }); //输出字符串 (*) string s2 = new string ( '0' , 10); //输出10个0 Console.WriteLine(s1); Console.WriteLine(s2); Console.WriteLine(5.ToString( "00000" )); //前面补0 string 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) 字符串赋初值的三种方法
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);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | string str = "张三,韩迎龙,李四,王五,马六,杨七" ; if (str.Contains( "韩迎龙" )) { Console.WriteLine( "查找到了" ); } else { Console.WriteLine( "没找到" ); } |
->模拟Contains的实现(很局限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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的算法(很局限)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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)跑马灯的效果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | 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)接收用户输入的字符串,将其中的字符以相反的顺序输出"韩迎龙"->"龙迎韩"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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日"从日期字符串中把年月日分别取出来,打印到控制台
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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]); } } |
初心商城:初心商城
作者:韩迎龙(Kencery) MVC/.NET群:159227188如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏一杯咖啡,本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构