C#字符串类String的使用(一)

概述

在c#中字符串做为对象来处理,能够经过String类来建立字符串对象。字符串必须包含在一对 " "(双引号)以内。"1122" 、"nihao".c#

声明字符串常量

注:声明字符串变量必须通过初始化才能使用,不然编译器会报出“使用了未赋值的变量”。code

string s;
链接多个字符串
//链接多个字符串
            String s1 = "hello";
            String s2 = "World";
            String s = s1 + "" + s2;
            Console.WriteLine(s);
            Console.ReadLine();
比较字符串
//字符串的比较
            //结果应为false
            string str1 = "zhzfdx";
            string str2 = "zhzfdx@qq.com";
            Console.WriteLine((str1 == str2));
            Console.ReadLine();
//运用compare方法
            //相等为0 小于-1 大于1
            //若长度相等比较比较字符串按照字典排序的规则,
            //在英文字典中前面的单词小于后面的单词
            string str1 = "zhzfdx";
            string str2 = "zhzfdx@qq.com";
            Console.WriteLine(String.Compare(str1, str1));
            Console.WriteLine(String.Compare(str1, str2));
            Console.WriteLine(String.Compare(str2, str1));
            Console.ReadLine();
//CompareTo方法:
            //以实例对象自己与指定字符串做比较

            Console.WriteLine(str1.CompareTo(str2));
            Console.ReadLine();
//Equals方法:主要用于比较两个字符串是否相同
            //返回true 或者false
            Console.WriteLine(str1.Equals(str2));
            Console.WriteLine(String.Equals(str1, str1));
            Console.WriteLine(String.Equals(str1, str2));
            Console.ReadLine();

未完待续。。。。。。对象

相关文章
相关标签/搜索