黑马程序员.net基础五

来源:互联网 发布:ios 淘宝hd 编辑:程序博客网 时间:2024/05/16 04:06
---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------


命名空间(namespace

 用于解决类重名问题,可以看做类的文件夹

如果代码和被使用的类在一个namespace则不需要using

在不同命名空间下的类调用有两种方法: 

1)写全称  命名空间.类名。

2)先using引用命名空间,在调用。

 

 

string(字符串的处理)

string可以看做是char的只读数组,char c = s[1]

例子:遍历输出string中的每个元素。

 

string s = "hello,world!";  

            for (int i = 0; i < s.Length;i++ )//遍历字符串中的字符。   

            {  

                Console.WriteLine(s[i]);  

            }  

string s = "hello,world!";

            for (int i = 0; i < s.Length;i++ )//遍历字符串中的字符。

            {

                Console.WriteLine(s[i]);

            }

 

c#中字符串有一个重要的特性:不可变性,字符串一旦声明就不再可以改变,所以只能通过索引来读取指定位置的char,且不能对指定位置的char进行修改。

如果要对char进行修改,那么就必须创建一个新的字符串,用s.ToCharArray()方法得到字符串的char数组,对数组进行修改后,调用new stringchar[])这个构造函数来创建char数组的字符串。一旦字符串被创建,那么char数组的修改也不会造成字符串的变化。

例子:将字符串中的A替换为a

 

char[] newchar = s.ToCharArray();//利用s.ToCharArray()来修改字符串中某个具体的字符。   

           newchar[0] = 'H';//将字符串S中的'h''H'替换   

           Console.WriteLine(newchar);  

 char[] newchar = s.ToCharArray();//利用s.ToCharArray()来修改字符串中某个具体的字符。

            newchar[0] = 'H';//将字符串S中的'h''H'替换

            Console.WriteLine(newchar);

 

注意:一切类型都可以调用.tostring()方法价转换成字符串类型。

      字符串的连接+,两边只要有一个是字符串类型,另一个也会被自动转换成字符串类型。

习题:两个学院输入各自喜欢的课程名称,判断是否一致,如果相同,则输出你俩喜欢的课程相同,如果不同,则输出你们俩喜欢的课程不相同。

   

Console.WriteLine("请输入你喜欢的课程:");  

          string a1 = Console.ReadLine();  

          Console.WriteLine("请输入你喜欢的课程:");  

          string a2 = Console.ReadLine();  

          if (a1.Equals(a2, StringComparison.OrdinalIgnoreCase))  

          {  

              Console.WriteLine("你俩喜欢的课程相同");  

          }  

          else   

          {  

              Console.WriteLine("你俩喜欢的课程不同");  

          }  

  

  

          Console.ReadKey();  

  Console.WriteLine("请输入你喜欢的课程:");

            string a1 = Console.ReadLine();

            Console.WriteLine("请输入你喜欢的课程:");

            string a2 = Console.ReadLine();

            if (a1.Equals(a2, StringComparison.OrdinalIgnoreCase))

            {

                Console.WriteLine("你俩喜欢的课程相同");

            }

            else 

            {

                Console.WriteLine("你俩喜欢的课程不同");

            }

            Console.ReadKey();

 string类型相关语句

字符串忽略大小写的比较

将字符串中的所有字母转换成大写:ToUpper();       

      将字符串中的所有字母转换成小写:ToLower();

 str1.Equels(str2,StringComparison.OrdinalIgnoreCase) 

字符串的分割

string[]Split(params char[]separator):将字符串按照指定的分割符分割为字符串数组。 

string[]Split(char[]separator,StringSplitOptions.RemoveEmptyEntries)将字符串按照指定的char分隔符分割为字符串数组(optionsRemoveEmptyEntries的时候移除结果中的空白字符串)

 练习:

让用户输入一个日期格式如:2008-01-02;你输出的日期为200812日。(只练习replace,不考虑其他因素)

 

Console.WriteLine("XXXX-XX-XX格式来输入你要转换的日期:");  

           string result = Console.ReadLine();  

           string result1 = result.Replace("-", "");  

           Console.WriteLine("转换后的日期为:{0}", result1);  

 Console.WriteLine("XXXX-XX-XX格式来输入你要转换的日期:");

            string result = Console.ReadLine();

            string result1 = result.Replace("-", "");

            Console.WriteLine("转换后的日期为:{0}", result1);

string.Indexof (string) :指定字符在此实例中的第一个匹配项的索引

例:

string str = "BCDEAFGAHIAJKALMNAOPQ";  

            

            Console.WriteLine( str.IndexOf("A"));  

string str = "BCDEAFGAHIAJKALMNAOPQ";

          

            Console.WriteLine( str.IndexOf("A"));取子字符串:string Substring(int startindex),取从位置startindex开始一直到最后的子字符串。

bool Contains (string value)判断字符串中是否含有子串value

bool StartWith(string value) 判断字符串是否以子串value开始

bool EndsWith(string value) 判断字符串是否以子串value结束



---------------------- <a href="http://edu.csdn.net"target="blank">ASP.Net+Android+IOS开发</a>、<a href="http://edu.csdn.net"target="blank">.Net培训</a>、期待与您交流! ----------------------

原创粉丝点击