C#中String类的简单应用
2014-02-20 09:05
423 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace String类àà { class Program { static void Main(string[] args) { string str1 = "this is my first test about Strnig"; string str2 = "this is the second test"; Console.WriteLine("========================string 类中compare的应用=================="); Console.WriteLine("str1和str2分别是" + str1 + " " + str2); Console.WriteLine("str1与str2比较的结果是: " + str1.CompareTo(str2)); Console.WriteLine("str1的长度" + str1.Length); Console.WriteLine("=======================string中contains的用ó?法·¨====================="); string str3 = "written by majianjie"; string str4 = "written by majianjie too"; Console.WriteLine("str3和oístr4测的结果是:" + str4.Contains(str3)); Console.WriteLine("=======================string 中equals的用法·¨======================"); string str5 = str4; Console.WriteLine("equals比较的结果是: " + str4.Equals(str5)); Console.WriteLine("=======================string中copyto的用法·¨========================"); string str6 = "majianjiemajianjiemajianjie"; char[] ch1 = new char[str6.Length]; str6.CopyTo(0, ch1, 0, 5); foreach (char ontChar in ch1) { Console.Write("{0}", ontChar); } Console.WriteLine(); Console.WriteLine("========================string中Dstart的用法·¨==================================="); string str7 = "my name is majianjie, i am in qinhuangdao, shagndaxue...oo, shit!!"; Console.WriteLine("str7字符串是: "+str7); Console.WriteLine("判D断是否以\"my\"开a始的" + str7.StartsWith("my") + " 和是否以\"!!\"结束的 " + str7.EndsWith("!!")); Console.WriteLine("========================string 中Dindex的用法·¨======================="); Console.WriteLine("str7中D\"am\"出现的为a位置是: " + str7.IndexOf("m") + " str7中D\"qinhuangdao\"第1次出现的位置?: " + str7.LastIndexOf("qinhuangdao")); Console.WriteLine("===================string 中indexofany的用法和lastindexofany的用法·¨==============="); Console.WriteLine("str7 的长度是: "+str7.Length); char[] ch = { 'q','i'}; int a = str7.IndexOfAny(ch); //最早出现q或者i的位置 Console.WriteLine("indexofany q i :"+a); char[] ch2 = { 'i','t'}; int b = str7.LastIndexOfAny(ch2); //最后出现i或者t的位置 Console.WriteLine("lastindexofany i t :"+b); Console.WriteLine("===============string中substring的用法·¨============================="); Console.WriteLine(str7.Substring(64)); Console.WriteLine(str7.Substring(60,6)); Console.WriteLine("=================string中拆分字符串的用法·¨==========================="); string chaifen = "majianjie1:22|majianjie2:23|majianjie3:24|majianjie4:25"; Console.WriteLine(chaifen); Console.WriteLine(); //string x = "\\|"; string[] result1=chaifen.Split(("\\|").ToCharArray()); for (int i = 0; i < result1.Length;i++ ) { Console.WriteLine(result1[i]); } Console.WriteLine(); for (int i = 0; i < result1.Length; i++) { string[] result2 = result1[i].Split((":".ToCharArray())); Console.WriteLine(result2[0]+"-->"+result2[1]); } Console.WriteLine("================string中replace的用法·¨===================="); Console.WriteLine(chaifen.Replace("jie","kang")); Console.WriteLine(); Console.WriteLine("============string中DTrim--trimstart--Trimend--toupper--tolower的用法·¨==============="); string str8 = " ma jiaNjIe M ma JiANjie MAJIAN JiE ie "; Console.WriteLine("原型: "+str8); Console.WriteLine(str8.ToLower()); Console.WriteLine(str8.ToUpper()); Console.WriteLine(str8.Trim()); Console.WriteLine(str8.TrimEnd()); Console.WriteLine(str8.TrimStart()); Console.WriteLine("=======================StringBuffer的用法·¨========================="); StringBuilder sb = new StringBuilder(); sb.Append("this is my first test"); sb.Append("11111111111111"); Console.WriteLine(sb); Console.WriteLine("=======================AppendFormat的用法·¨========================="); StringBuilder strBu = new StringBuilder(); strBu.AppendFormat("111 {0} {1}", '\n', DateTime.Now.ToString("HH:mm:ss")); Console.WriteLine(strBu); string []value={"1","2","3"}; string c=""; StringBuilder str10=new StringBuilder(); foreach(string text in value) { str10.AppendFormat(",{0}", text); //将value数组中的值拼接成一个字符串,以逗号分隔 } if(str10!=null&&str10.Length>0) { str10.Remove(0,1);//移除第一个逗号 } c=str10.ToString(); Console.WriteLine(c); Console.ReadLine(); } } }
相关文章推荐
- [转]使用C#开发一个简单的P2P应用
- 使用C#开发一个简单的P2P应用
- C# webservice应用 简单实例
- C# 委托与事件简单应用
- C# Event 和 Delegate 的简单应用
- 使用C#开发一个简单的P2P应用
- 在C#代码中应用Log4Net(一)简单使用Log4Net
- C# 类、属性的简单应用操作
- 使用C#开发一个简单的P2P应用
- Windows 系统调用.COM.微软语言引擎简单应用(C#)
- C# 接口属性的定义&get、set访问器的简单应用
- WinForm(C#) DataGridView简单应用示例
- C#使用DataSet类、DataTable类、DataRow类、OleDbConnection类、OleDbDataAdapter类编写简单数据库应用
- 使用C#开发一个简单的P2P应用
- C# 工厂的简单应用
- c# .net windows消息队列简单应用示例
- C#中线程池的简单应用
- 【C#学习】聊天机器人,网络数据的简单应用
- 使用C#开发一个简单的P2P应用
- C# .NET Remoting 简单应用示例