字符串

字符串是直接从 object 继承的密封类类型。不可以被继承,表示 Unicode 字符串。
前面我们还了解了可以使用@表示禁止转义。

字符串常用属性

Length 返回字符串长度
Chars 指定字符位置的字符


字符串常用方法

IndexOf
IndexOfAny
LastIndexOf
LastIndexOfAny

字符的索引
Insert 指定索引位置插入一个指定的 String 实例
Remove 从此实例中的指定位置开始删除指定数目的字符  
PadLeft
PadRight
使用空格或指定的字符填充
Replace 使用新的字符替换原有的字符
Split 按指定的分隔符将字符串转为数组
Substring 查询子串
ToCharArray 将字符串转为字符数组
ToLower
ToUpper
大小写转换
IsNullOrEmpty 是空还是非空字符串
Trim
TrimEnd
TrimStart
去除空格或指定字符

string的本质是char的数组描述形式,可以通过以下的代码来证实
 1            string s = "Hello C#";
 2
 3            foreach (char c in s)//string中的每个元素都是char
 4            {
 5                System.Console.WriteLine(c);
 6            }

 7
 8            for (int i = 0; i <= s.Length - 1;i++ )//string就是一个char数组
 9            {
10                System.Console.WriteLine(s[i]);
11            }

  很多同学对indexof这个方法总感觉莫名其妙,不知道indexof有什么用。indexof有很多地方可以使用。
现看一下indexof的表现
1            string s = "Hello C#";
2
3            //仅仅是查找的方向不一样,字符的位置总是从0开始计算
4            System.Console.WriteLine(s.IndexOf('l'));//2,第3个字符,从前面数第一个l
5            System.Console.WriteLine(s.LastIndexOf('l'));//3,第4个字符,从后面数第一个l

那indexof和lastindexof有什么具体的作用呢?
 1            string s = "-12.23";
 2
 3            if(s.IndexOf('.')!=s.LastIndexOf('.'))
 4            {
 5                System.Console.WriteLine("小数点不止一个,错误");
 6            }

 7
 8
 9            if(s.LastIndexOf('-')>0)
10            {
11                System.Console.WriteLine("符号不止一个或符号不在第一位,错误");
12            }

13
14
15            if (s.LastIndexOf('.'- s.LastIndexOf('-')==1)
16            {
17                System.Console.WriteLine("小数点在符号直接后面,错误");
18            }

其实indexof和lastindexof的处理大致如下

 1            string s = "hello";
 2
 3            //indexof
 4            for (int i = 0; i <= s.Length - 1;i++ )
 5            {
 6                if(s[i]=='l')
 7                {
 8                    System.Console.WriteLine("indexof={0}",i);
 9                    break;
10                }

11            }

12
13           //lastindexof
14            for (int i = s.Length-1; i >=0; i--)
15            {
16                if (s[i] == 'l')
17                {
18                    System.Console.WriteLine("lastindexof={0}", i);
19                    break;
20                }

21            }



我们来个练习巩固一下

声明一个字符串,且赋值” 我们使用Microsoft Visual Studio .NET 2005 C#开发新一代的应用程序”。
输出 英文字符的个数
输出 标点符号的个数
输出 剩余其他字符的个数

 1            char[] c = "我们使用Microsoft Visual Studio .NET 2003 C#开发新一代的应用程序".ToCharArray();
 2            string letter = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 3            letter += letter.ToLower();
 4            string punctuation = @"~!@#$%^&*()_+|`-=\[]{};':<>?,./";
 5            int li = 0;//英文字符个数
 6            int pi = 0;//标点符号个数
 7            int ci = 0;//其他字符个数
 8            for (int i = 0; i <= c.Length - 1; i++)
 9            {
10                if (letter.IndexOf(c[i]) >= 0)
11                
12                    li++
13                }

14                else
15                {
16                    if (punctuation.IndexOf(c[i]) >= 0)
17                    {
18                        pi++
19                    }

20                    else
21                    
22                        ci++
23                    }

24                }

25            }

26            System.Console.WriteLine("总字符个数:{0}", c.Length);
27            System.Console.WriteLine("英文字符个数:{0}标点符号个数:{1}其他字符个数:{2}合计:{3}",
28                        li, pi, ci, li + pi + ci);


当然,我们可以用char的静态方法来优化上面的代码
 1            char[] c = "我们使用Microsoft Visual Studio .NET 2003 C#开发新一代的应用程序".ToCharArray();
 2            int li = 0;//英文字符个数
 3            int pi = 0;//标点符号个数
 4            int ci = 0;//其他字符个数
 5
 6            for (int i = 0; i <= c.Length - 1; i++)
 7            {
 8                if (char.IsUpper(c[i]) || char.IsLower(c[i]))
 9                {
10                    li++;
11                }

12                else
13                {
14                    if (char.IsPunctuation(c[i]))
15                    {
16                        pi++;
17                    }

18                    else
19                    {
20                        ci++; ;
21                    }

22                }

23            }

24            System.Console.WriteLine("{0}--{1},{2},{3}:{4}", li + pi + ci, li, pi, ci, c.Length);

spilt是非常有用的方法

 1            string dir = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\AppLaunch.exe";
 2
 3
 4            //不使用Split的繁琐代码
 5
 6            int star = 0;
 7            int end = dir.IndexOf('\\', star);
 8
 9            do
10            {
11                System.Console.WriteLine(dir.Substring(star, end - star));
12                star = end + 1;
13                end = dir.IndexOf('\\', star);
14            }
 while (end > -1);
15            System.Console.WriteLine(dir.Substring(star));
16
17
18            //不使用Split的优雅代码
19            string[] dirs = dir.Split('\\');
20            foreach (string d in dirs)
21            {
22                System.Console.WriteLine(d);
23            }

24
25            //直接定位文件
26            System.Console.WriteLine(dir.Split('\\')[dir.Split('\\').Length - 1]);
原文地址:https://www.cnblogs.com/shyleoking/p/635466.html