C#字符串

it2025-10-06  3

创建

using System; namespace StringApplication { class Program { static void Main(string[] args) { //字符串,字符串连接 string fname, lname; fname = "Rowan"; lname = "Atkinson"; string fullname = fname + lname; Console.WriteLine("Full Name: {0}", fullname); //通过使用 string 构造函数 char[] letters = { 'H', 'e', 'l', 'l','o' }; string greetings = new string(letters); Console.WriteLine("Greetings: {0}", greetings); //方法返回字符串 string[] sarray = { "Hello", "From", "Tutorials", "Point" }; string message = String.Join(" ", sarray); Console.WriteLine("Message: {0}", message); //用于转化值的格式化方法 DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1); string chat = String.Format("Message sent at {0:t} on {0:D}", waiting); Console.WriteLine("Message: {0}", chat); Console.ReadKey() ; } } }

编译结果:

Full Name: RowanAtkinson Greetings: Hello Message: Hello From Tutorials Point Message: Message sent at 17:58 on Wednesday, 10 October 2012

属性

属性描述Chars在当前 String 对象中获取 Char 对象的指定位置。Length在当前的 String 对象中获取字符数。

方法

string s1 = "00wqw12"; string s2 = "0012"; Console.WriteLine(string.Compare(s1, s2)); //1第一个不相同的字符大小 Console.WriteLine(string.Concat(s1,s2)); //连接 Console.WriteLine(string.Copy(s1)); //copy Console.WriteLine(string.Equals(s1,s2)); //相等 Console.WriteLine(string.IsNullOrEmpty(s1));//为空 Console.WriteLine(string.IsNullOrWhiteSpace(s1));//空或空格 Console.WriteLine(string.Join(",", new string[] { s1, s2 }));//用,连接 Console.WriteLine(s1.Clone()); //clone Console.WriteLine(s1.Contains(s2)); //包含 Console.WriteLine(s1.Count()); //数量 Console.WriteLine(s1.Insert(1,s2)); //插入 Console.WriteLine(s1.Length); Console.WriteLine(s1.PadLeft(7,',')); //从左填充‘,’,使长度达到7 Console.WriteLine(s1.Remove(1)); //从1开始删除后面所有 Console.WriteLine(s1.Remove(1,2)); //从1开始删除两个字符 Console.WriteLine(s1.Replace('0','_')); //0字符替换为_字符 Console.WriteLine(s1.Replace("0","_")); //0字符串替换为_字符串 Console.WriteLine(s1.Reverse()); //反转字符串 Console.WriteLine(s1.Split(',')); //分割字符串成一个字符数组 Console.WriteLine(s1.Substring(2,3)); //从2开始截取三个字符 Console.WriteLine(s1.ToCharArray()); //字符串转为字符数组 Console.WriteLine(s1.ToLower()); //小写 Console.WriteLine(s1.IndexOf('0')); //第一个字符0的位置 Console.WriteLine(s1.IndexOf("00")); //第一个字符串 Console.WriteLine(s1.LastIndexOf('0')); //最后一个
最新回复(0)