04---Net基础加强

字符串常用方法:

属性: Length获取字符串中字符的个数

IsNullOrEmpty()   静态方法,判断为null或者为“”

ToCharArray() 将string转换为char[]

ToLower()   小写,必须接收返回值(因为字符串的不变性)

ToUpper()  大写

Equals()  比较两个字符串是否相同,忽略大小写的比较,StringComparation

IndexOf() 如果没有找到对应的数据,返回-1

LastIndexOf() 如果没有找到对应的数据,返回-1

Substring()   //2个重载,截取字符串

Split()   //分割字符串

Join()    静态方法

Format()  静态方法

Repalce()

 

举例: IsNullOrEmpty()   静态方法,判断为null或者为“”

class Program
    {
        static void Main(string[] args)
        {
            //string msg = "你好,China";
            //string msg = "";
            string msg = null;//  msg.Length后面会报错
            //判断字符串是否为空
            //if (msg == "")
            if (string.IsNullOrEmpty(msg))
            {
                Console.WriteLine("空字符串!!!!");
            }
            else 
            {
                 Console.WriteLine("非空字符串!!!!");
            }
            Console.WriteLine(msg.Length);
            Console.ReadKey();
        }
    }

 

字符串的不可变性

    class Program
    {
        static void Main(string[] args)
        {
            //字符串的不可变性指的是字符串一旦声明就不可改变
            string s1 = "abc";
            s1 = s1 + "d";
            Console.WriteLine(s1);   //输出abcd
            Console.ReadKey();
        }
    }

 

ToUpper()  大写

    class Program
    {
        static void Main(string[] args)
        {
            //string msg = "hEllo";
            ////字符串修改完成后必须接收返回值,因为字符串具有不可变性,无法直接修改原来的字符串
            //msg=msg.ToUpper();
            //Console.WriteLine(msg);
            //Console.ReadKey();
        }
    }

 

为什么字符串中的equals方法和==不能判断两个对象是否相同呢?因为字符串中有一个equals方法时判断字符串只要每个字符相同就返回true,并且字符串类型对运算符==进行了重载,也是调用的equals()方法!所以通过equals方法和==不能判断两个对象是否相同!

  class Program
    {
        static void Main(string[] args)
        {
            string s1 = "abc";
            string s2 = "abc";
            Console.WriteLine(s1.Equals(s2));  //true
            Console.WriteLine(s1 == s2);         //true
            Console.WriteLine(object.ReferenceEquals(s1, s2));  //true
            Console.ReadKey();
        }
    }

 

此时s1和s2确实为同一个对象, string s1 = "abc";  string s2 = "abc"; 这是因为字符串的“暂存池”(字符串池,字符串拘留池)特性 与下面的
string s1 = new string(new char[]{'a','b','c'});
string s2 = new string(new char[] { 'a','b','c' });
效果不一样,只要出现了new关键字,就表示一定会创建一个新对象
class Program
    {
        static void Main(string[] args)
        {
            string s1 = new string(new char[]{'a','b','c'});
            string s2 = new string(new char[] { 'a','b','c' });
            Console.WriteLine(s1.Equals(s2));  //true
            Console.WriteLine(s1 == s2);         //true
            Console.WriteLine(object.ReferenceEquals(s1, s2));  //false
            Console.ReadKey();
        }
    }

 

IndexOf()  LastIndexOf() 

class Program
    {
        static void Main(string[] args)
        {
            string msg = "我爱你北京天安门,天安门上太阳升。我家不住天安门,天安门上有保安";
            int index = msg.IndexOf("天安门");     //5
            //int index = msg.LastIndexOf("天安门");  //25
            Console.WriteLine(index);
            Console.ReadKey();
        }
    }

 

Substring()   //截取字符串,2个重载,没有传递截取长度则截取到最后

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "听说过叶长种吗?";
            //从坐标为3的索引开始截取,截取长度为3
            msg=msg.Substring(3,3);
            Console.WriteLine(msg); // 结果为叶长种
            Console.ReadKey();
        }
    }

 

Split()   //分割字符串

 class Program
    {
        static void Main(string[] args)
        {
            string msg = "乔丹|科比|叶长种";
            string[] name=msg.Split('|');
            for (int i = 0; i < name.Length; i++)
            {
                Console.WriteLine(name[i]);
            }          
            Console.ReadKey();
        }
    }

 

 class Program
    {
        static void Main(string[] args)
        {
            string msg = "乔丹|科比|叶长种||||||||詹姆斯";
            string[] name=msg.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < name.Length; i++)
            {
                Console.WriteLine(name[i]);
            }          
            Console.ReadKey();
        }
    }

 

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "乔丹|科比|叶长种||||||||詹姆斯";
            string[] name = msg.Split(new char[] { '|' }, 3, StringSplitOptions.RemoveEmptyEntries);//前截取三个  乔丹  科比  叶长种||||||||詹姆斯
            for (int i = 0; i < name.Length; i++)
            {
                Console.WriteLine(name[i]);
            }
            Console.ReadKey();
        }
    }

 

Join()    静态方法

   class Program
    {
        static void Main(string[] args)
        {
            string msg = "乔丹|科比|叶长种||||||||詹姆斯";
            string[] name=msg.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);
            string full = string.Join("=====>",name);
            Console.WriteLine(full);
            Console.ReadKey();
        }
    }

 

Format()  静态方法

 class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("我叫{0},今年{1}岁了,至今{2}","叶长种",25,"未婚");
            //Console.ReadKey();
            string msg = string.Format("我叫{0},今年{1}岁了,至今{2}", "叶长种", 25, "未婚");
            Console.WriteLine(msg);
            Console.ReadKey();
        }
    }

 

Repalce()

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "大家知道传值与引用的区别吗?请来问叶长重吧";
            msg = msg.Replace('', '');
            Console.WriteLine(msg);//输出:大家知道传值与引用的区别吗?请来问叶长种吧
            Console.ReadKey();
        }
    }

 

 class Program
    {
        static void Main(string[] args)
        {
            string msg = "大家知道传值与引用的区别吗?请来问叶长重吧,哈哈哈哈";
            msg = msg.Replace('', '').Replace('', '');
            Console.WriteLine(msg);
            Console.ReadKey();
        }
    }

 

字符串练习

输入一个字符串将其字符串以相反的顺序输出如abc输出cba

 class Program
    {
        static void Main(string[] args)
        {
            //接收用户输入的字符串,将其中的字符以与输入相反的顺序输出。
            Console.WriteLine("请输入一个字符串:");
            string msg = Console.ReadLine();
            msg = ReverseString(msg);
            //for (int i = msg.Length-1; i >=0; i--)
            //{
            //    Console.Write(msg[i]); 
            //}
            Console.Write(msg); 
            Console.ReadKey();
        }

        private static string ReverseString(string msg)
        {
            Char[] ch = msg.ToCharArray();
            for (int i = 0; i < ch.Length/2; i++)
            {
                char temp = ch[i];
                ch[i] = ch[ch.Length - 1 - i];
                ch[ch.Length - 1 - i] = temp;
            }
            return new string(ch);
        }

 

接收用户输入的一句英文,把里面的单词以相反的顺序输出如 I Love You 输出 uoY evoL I

  class Program
    {
        static void Main(string[] args)
        {
            //接收用户输入的一句英文,将其中的单词以与输入相反的顺序输出。
            Console.WriteLine("请输入一句话:");
            string msg = Console.ReadLine();
            string[] words = msg.Split(' ');
            for (int i = 0; i < words.Length; i++)
            {
                words[i] = ReverseString(words[i]);
            }
            msg = string.Join(" ", words);
            Console.Write(msg);
            Console.ReadKey();
        }

        private static string ReverseString(string msg)
        {
            Char[] ch = msg.ToCharArray();
            for (int i = 0; i < ch.Length / 2; i++)
            {
                char temp = ch[i];
                ch[i] = ch[ch.Length - 1 - i];
                ch[ch.Length - 1 - i] = temp;
            }
            return new string(ch);
        }
    }

 

分割年月日

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入日期:");
            string date = Console.ReadLine();
            string[] parts = date.Split('', '', '');
            Console.WriteLine("年:{0}", parts[0]);
            Console.WriteLine("月:{0}", parts[1]);
            Console.WriteLine("日:{0}", parts[2]);
            Console.ReadKey();
        }
    }

 

把csv文件中的联系人姓名和电话号码显示出来

 class Program
    {
        static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("info.csv",Encoding.Default);
            //循环遍历每一行
            for (int i = 0; i < lines.Length; i++)
            {
               string[] parts= lines[i].Replace("\"","").Split(','); 
               Console.WriteLine("姓名:{0};电话{1}", parts[0] + parts[1], parts[2]);
            }
            Console.ReadKey();
        }
    }

 

123-456---7---89-----123----2把类似的字符串中重复符号”-”去掉,既得到123-456-7-89-123-2. split()、

 class Program
    {
        static void Main(string[] args)
        {
            string msg = "123-456---7---89-----123----2";
            string[] parts = msg.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
            string str = string.Join("-", parts);
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }

 

从文件路径中提取出文件名(包含后缀) 。比如从c:\a\b.txt中提取出b.txt这个文件名出来。

    class Program
    {
        static void Main(string[] args)
        {
            string path = @"c:\a\b.txt";
            // 查找最后一个\出现的索引位置
            int index = path.LastIndexOf('\\');
            string filename = path.Substring(index + 1);
            Console.WriteLine(filename);
            Console.ReadKey();
        }
    }

 

192.168.10.5[port=21,type=ftp]     192.168.10.5[port=21]

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "192.168.10.5[port=21,type=ftp]";
            string[] parts = msg.Split(new string[] { "[port=", ",type=", "]" }, StringSplitOptions.RemoveEmptyEntries);
            Console.WriteLine("IP:{0}\r\n  Port:{1} \r\n service:{2}", parts[0], parts[1], parts.Length == 3 ? parts[2] : "http");
            Console.ReadKey();
        }
    }

 

求员工工资文件中,员工的最高工资、最低工资、平均工资

    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = File.ReadAllLines("salary.txt", Encoding.Default);
            string[] parts = lines[0].Split('=');

            //假设第一个人的工资是最高工资
            string maxName = parts[0];
            int maxSalary = Convert.ToInt32(parts[1]);

            //假设第一个人的工资是最低工资
            string minName = parts[0];
            int minSalary = Convert.ToInt32(parts[1]);

            int sum = minSalary;//用来存储总工资。

            int count = 1;

            //循环遍历其他人的工资进行比对,计算最高工资与最低工资
            for (int i = 1; i < lines.Length; i++)
            {
                //跳过空行
                if (lines[i].Length != 0)
                {
                    count++;
                    string[] lineParts = lines[i].Split('=');
                    int salary = Convert.ToInt32(lineParts[1]);

                    //进行最高工资的比较
                    if (salary > maxSalary)
                    {
                        maxSalary = salary;
                        maxName = lineParts[0];
                    }

                    //进行最低工资的比较
                    if (salary < minSalary)
                    {
                        minSalary = salary;
                        minName = lineParts[0];
                    }
                    sum += salary;
                }

            }

            Console.WriteLine("最高工资:{0}  最低工资:{1}", maxSalary, minSalary);
            Console.WriteLine("平均工资:{0}", sum * 1.0 / count);
            Console.ReadKey();
        }
    }

 

 “北京传智播客软件培训,传智播客.net培训,传智播客Java培训。传智播客官网:http://www.itcast.cn。北京传智播客欢迎您。”。在以上字符串中请统计出”传智播客”出现的次数。找IndexOf()的重载。 
    class Program
    {
        static void Main(string[] args)
        {
            string msg = "北京传智播客软件培训,传智播客.net培训,传智播客Java培训。传智播客官网:http://www.itcast.cn。北京传智播客欢迎您。";
            string defaultWord = "传智播客";
            int count = 0;
            int index = 0;

            while ((index = msg.IndexOf(defaultWord, index)) != -1)
            {
                count++;
                index = index + defaultWord.Length;
            }
            Console.WriteLine("次数是:{0}", count);
            Console.ReadKey();
        }
    }

 

面向对象计算器-抽象类版本

抽象计算器类

 /// <summary>
    /// 计算器的一个父类
    /// </summary>
    public abstract class JiSuanQi   // 这个类必须定义为抽象类 前面加abstract
    {
        public JiSuanQi(int n1, int n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public int Number1
        {
            get;
            set;
        }

        public int Number2
        {
            get;
            set;
        }

        public abstract double JiSuan();
    }

 

加法类:

  /// <summary>
    /// 计算加法的类
    /// </summary>
    public class Add : JiSuanQi
    {
        public Add(int n1, int n2)
            : base(n1, n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double JiSuan()
        {
            return this.Number1 + Number2;
        }
    }

 

减法类:

    /// <summary>
    /// 计算减法的类
    /// </summary>
    public class Sub : JiSuanQi
    {
        public Sub(int n1, int n2)
            : base(n1, n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double JiSuan()
        {
            return this.Number1 - Number2;
        }
    }

 

乘法类:

 /// <summary>
    /// 计算乘法的类
    /// </summary>
    public class Mult : JiSuanQi
    {
        public Mult(int n1, int n2)
            : base(n1, n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double JiSuan()
        {
            return this.Number1 * Number2;
        }
    }

 

除法类

   /// <summary>
    /// 计算除法的类
    /// </summary>
    public class Div : JiSuanQi
    {
        public Div(int n1, int n2)
            : base(n1, n2)
        {
            this.Number1 = n1;
            this.Number2 = n2;
        }
        public override double JiSuan()
        {
            return this.Number1 / Number2;
        }
    }

 

主程序

    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                JiSuanQi jsq = null;
                Console.WriteLine("number1 :");
                int n1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("操作符");
                string czf = Console.ReadLine();
                Console.WriteLine("number2 :");
                int n2 = Convert.ToInt32(Console.ReadLine());
                switch (czf)
                {
                    case "+":
                        jsq = new Add(n1, n2);
                        break;

                    case "-":
                        jsq = new Sub(n1, n2);
                        break;

                    case "*":
                        jsq = new Mult(n1, n2);
                        break;

                    case "/":
                        jsq = new Div(n1, n2);
                        break;

                    default:
                        break;
                }
                if (jsq != null)
                {
                    double result=jsq.JiSuan();
                    Console.WriteLine("结果:{0}",result);
                }
            }
        }
    }

 

提取方法:

    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                JiSuanQi jsq = null;
                Console.WriteLine("number1 :");
                int n1 = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("操作符");
                string czf = Console.ReadLine();
                Console.WriteLine("number2 :");
                int n2 = Convert.ToInt32(Console.ReadLine());
                jsq = GetJiSuan(n1, czf, n2);
                if (jsq != null)
                {
                    double result=jsq.JiSuan();
                    Console.WriteLine("结果:{0}",result);
                }
            }
        }

        //简单工厂设计模式
        private static JiSuanQi GetJiSuan(int n1, string czf, int n2)
        {
            JiSuanQi jsq=null;
            switch (czf)
            {
                case "+":
                    jsq = new Add(n1, n2);
                    break;

                case "-":
                    jsq = new Sub(n1, n2);
                    break;

                case "*":
                    jsq = new Mult(n1, n2);
                    break;

                case "/":
                    jsq = new Div(n1, n2);
                    break;

                default:
                    break;
            }
            return jsq;
        }
    }

 

值类型与引用类型:

 class Program
    {
        static void Main(string[] args)
        {
            //// 值类型, 栈
            ////int  short byte char bool double float struct enum decimal 

            ////引用类型 string 数组 类 接口 委托 
            ////堆
            //string s = "a";
            //s = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";

            ////值类型
            //int n = 100;
            //int m = n;
            //m = m + 1;
            //Console.WriteLine(n);  // 100
            //Console.ReadKey();

            //引用类型
            Person p = new Person();
            p.Age = 100;
            Person p1 = p;
            p1.Age = 120;
            Console.WriteLine(p.Age); //120
            Console.ReadKey();
        }
    }

    public class Person
    {
        public int Age
        {
            get;
            set;
        }
    }

 

练习1

  class Program
    {
        static void Main(string[] args)
        {
            //int n = 10;
            //M1(n);
            //Console.WriteLine(n);//10
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M2(p);
            //Console.WriteLine(p.Age);//101
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M3(p);
            //Console.WriteLine(p.Age);//1000
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M4(p);
            //Console.WriteLine(p.Age);//100
            //Console.ReadKey();

            //string name = "科比";
            //M5(name);
            //Console.WriteLine(name);//科比
            //Console.ReadKey();

            //int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //M6(arrInt);
            //for (int i = 0; i < arrInt.Length; i++)
            //{
            //    Console.WriteLine(arrInt[i]); // 1, 2, 3, 4, 5, 6, 7, 8
            //}
            //Console.ReadKey();   

            //int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //M7(arrInt);
            //for (int i = 0; i < arrInt.Length; i++)
            //{
            //    Console.WriteLine(arrInt[i]); //100,100,100,100,100,100,100,100
            //}
            //Console.ReadKey();
        }

        private static void M7(int[] arrInt)
        {
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = 100;
            }
        }
        private static void M6(int[] arrInt)
        {
            arrInt = new int[arrInt.Length];
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = arrInt[i] * 2;
            }
        }


        private static void M5(string name1)
        {
            name1 = "乔丹";
        }

        private static void M4(Person p1)
        {
            p1 = new Person();
            p1.Age++;
        }

        private static void M3(Person p1)
        {
            p1.Age = 1000;
            p1 = new Person();
            p1.Age = 200;
        }
        private static void M2(Person p1)
        {
            p1.Age++;
        }
        private static void M1(int m)
        {
            m++;
        }
    }

    public class Person
    {
        public int Age
        {
            get;
            set;
        }
    }

 

练习2

    class Program
    {
        static void Main(string[] args)
        {
            //int n = 10;
            //M1(ref n);
            //Console.WriteLine(n);//11
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M2(ref p);
            //Console.WriteLine(p.Age);//101
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M3(ref p);
            //Console.WriteLine(p.Age);//
            //Console.ReadKey();

            //Person p = new Person();
            //p.Age = 100;
            //M4(ref p);
            //Console.WriteLine(p.Age);//1
            //Console.ReadKey();

            //string name = "科比";
            //M5(ref name);
            //Console.WriteLine(name);//乔丹
            //Console.ReadKey();

            //int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //M6(ref arrInt);
            //for (int i = 0; i < arrInt.Length; i++)
            //{
            //    Console.WriteLine(arrInt[i]); //0,0,0,0,0,0,0,0
            //}
            //Console.ReadKey();  

            //int[] arrInt = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //M7(ref arrInt);
            //for (int i = 0; i < arrInt.Length; i++)
            //{
            //    Console.WriteLine(arrInt[i]); //100,100,100,100,100,100,100,100
            //}
            //Console.ReadKey(); 
        }
        private static void M7(ref int[] arrInt)
        {
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = 100;
            }
        }
        private static void M6(ref int[] arrInt)
        {
            arrInt = new int[arrInt.Length];
            for (int i = 0; i < arrInt.Length; i++)
            {
                arrInt[i] = arrInt[i] * 2;
            }
        }
        private static void M5(ref string name2)
        {
            name2 = "乔丹";
        }
        private static void M4(ref Person p1)
        {
            p1 = new Person();
            p1.Age++;
        }
        private static void M3(ref Person p1)
        {
            p1.Age = 1000;
            p1 = new Person();
            p1.Age = 200;
        }
        private static void M2(ref Person p1)
        {
            p1.Age++;
        }
        //值传递,传递的是栈中的内容,(对于值类型,栈中的内容就是对应的数据。对于引用类型栈中内容就是对象的地址)
        //引用传递,传递的是栈本身的地址,多个变量名实际上指向的是同一个栈变量。
        //引用传递必须使用ref关键字修饰。在方法调用的时候传递参数的时候也必须加ref 关键字
        private static void M1(ref int m)
        {
            m++;
        }
    }
    public class Person
    {
        public int Age
        {
            get;
            set;
        }
    }

 

posted @ 2014-10-29 12:10  代码沉思者  阅读(272)  评论(0编辑  收藏  举报