(4)C#中字符串,数组,枚举类型

(一)字符串

(1)字符串属性:

namespace _18_字符串一
{
    class Program
    {
        static void Main(string[] args)
        {
            char c1 = 'a';//char有且只能有一个字符
            char c2 = 'b';
            string s1 = "a";//可以为长度0的空字符串。
            string s2 = "b";

            string s3 = "hello";
            char c3 = s3[1];
            Console.WriteLine(c3);//可以直接取字符,string可以看成是char的只读数组。
            
            for (int i = 0; i < s3.Length;i++ )
            {
                char a = s3[i];
                Console.WriteLine(a);
            }
            //字符串不可变性,不能对指定位置的char进行修改。如果要进行修改只能创建新的字符串来存放,那先获取char数组,修改后,赋值给一个新的字符串,构造函数(new string(char[]))。
            string s4 = "hello";
            char[] chars = s1.ToCharArray();//chars只是string s1字符数组的一份拷贝
            chars[0] = 'a';
            string s5 = new string(chars);//声明了2个字符串,s1未改变,s2为新字符串
            Console.WriteLine("s4:{0},s5:{1}", s4, s5);

            string s6 = "hello";
            string s7 = s2;//s7通过s6指向字符串
            s6 = "new";
            //修改了s6 s6指向new,s7指向hello
            Console.WriteLine("s6指向的值:{0},s7指向的值{1}",s6,s7);
            Console.ReadKey();
        }
    }
}
(2)字符串常用函数

字符串常用函数一:

namespace _19_字符串常用函数
{
    class Program
    {
        static void Main(string[] args)
        {
            //常用函数
            //tolower()转换为小写
            string s1 = "Hello";
            string s2 = s1.ToLower();
            Console.WriteLine(s2);

            //trim移除两边的空白
            string s3 = "  hello ";
            string s4 = s3.Trim();
            Console.WriteLine(s4);

            //忽然大小写相等性判断
            bool b = s1.Equals(s2, StringComparison.OrdinalIgnoreCase);
            Console.WriteLine(b);

            //字符串大小的比较
            int i = "abc".CompareTo("ABC");//返回值为int,大于则1,小于则返回-1,等于返回0
            Console.WriteLine(i);

            //字符串分割函数 split

            string s5 = "11,22,33,44,55|22";
            string[] strs1 = s5.Split(',','|');//将字符串按指定的字符分割成字符串数组


            string s6 = "我是韩国人我是中国人我是日本人";
            //string[] strs = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            string[] strs2 = s6.Split(new string[] { "我是" }, StringSplitOptions.RemoveEmptyEntries);
            foreach(string str in strs2)
            {
                Console.WriteLine(str);
            }
            Console.ReadKey();
        }
    }
}
字符串常用函数一练习

namespace _20_字符串常用函数练习
{
    class Program
    {
        static void Main(string[] args)
        {
            从日期字符串中("2014-8-7")分析出年月日
            //string time = "2014-8-7";
            //string[] strs = time.Split('-');
            //Console.WriteLine("{0}年{1}月{2}日",strs[0],strs[1],strs[2]);

            //从一个记录了学生成绩的文本文档,每个学生的成绩是一行,每行用|分割数据,用|分割的域分辨是姓名,年龄,成绩,写程序求出成绩最高学生的姓名和成绩,
            string[] lines = System.IO.File.ReadAllLines(@"...\学生成绩.txt", Encoding.Default);
            int Max = 0;
            string name = "";
            foreach(string line in lines)
            {
                string[] strs = line.Split('|');
                int chengji = Convert.ToInt32(strs[2]);
                if (Max < chengji)
                {
                    Max = chengji;
                    name = strs[0];
                }
            }
            Console.WriteLine("成绩最高的学生:{0},成绩:{1}",name,Max);
            Console.ReadKey();

        }
    }
}
字符串常用函数二

namespace _21_字符串常用函数二
{
    class Program
    {
        static void Main(string[] args)
        {
            //replace函数
            string s1 = "aaaabeeg";
            string s2 = s1.Replace("a", "b");
            Console.WriteLine(s2);

            //substring函数  截取从第N个开始
            string a1 = "http://www.baidu.com";
            string a2 = a1.Substring(7);
            string a3 = a1.Substring(11, 5);
            Console.WriteLine(a2);
            Console.WriteLine(a3);
            Convert.ToInt32(s2);

            //Contains 判断含有某个字符串
            string b1 = "aadfgag";
            if (b1.Contains("a"))
            {
                Console.WriteLine("含有a元素");
            }

            //StartsWith/endswith 判断是否什么字符串开头
            string c1 = "aadffggv";
            bool c2 = c1.StartsWith("a");
            Console.WriteLine(c2);

            string f = "你好,我是王某某";
            int i = f.IndexOf("我是");
            string s = "123";
            int i = s.Length;
            Console.WriteLine(i);
            Console.ReadKey();
        }
    }
}
字符串常用函数二练习

namespace _22_字符常用函数练习二
{
    class Program
    {
        static void Main(string[] args)
        {
            接收用户输入的字符串,将其中的字符以输入相反的输入输出。
            Console.WriteLine("请输入字符串按回车结束");
            string s1 = Console.ReadLine();
            if (s1.Length > 0)
            {
                for (int i = s1.Length - 1; i >= 0; i--)
                {
                    Console.Write(s1[i]);
                }
            }
            else
            {
                Console.WriteLine();
            }

            //接收用户输入的一句英文,将其中的单词以反序输入。“hello c sharp”->"sharp c hello",默认输入2个以上的单词
            string s2 = Console.ReadLine();
            string[] strs2 = s2.Split(' ');
            string s21 = "";
            for (int i = strs2.Length - 1; i > 0; i--)
            {
                s21 = s21 + strs2[i] + " ";
            }
            s21 = s21 + strs2[0];
            Console.WriteLine(s2);

            //从emial中提取出用户名和域名:acv@163.com
            string s3 = "ams@163.com";
            string[] strs3 = s3.Split('@');
            string name = strs3[0];
            string 域名 = strs3[1];
            int atIndex = s3.IndexOf("@");
            string name3 = s3.Substring(0, atIndex);
            string 域名3 = s3.Substring(atIndex + 1);
            Console.WriteLine("{0},{1}", name3, 域名3);

            //文本文件中存储了多个文章标题,作者,标题和作者之间用若干个空格隔开,每行一个,标题有长有短的,输入到控制台的最多标题长度20,如果超过20,则截取长度17的子串并且最后添加“...”,加一个竖线最后出处作者的名字,默认文章标题不含空格
            string[] lines4 = System.IO.File.ReadAllLines(@"...\文档.txt", Encoding.Default);
            foreach (string line4 in lines4)
            {
                string[] strs4 = line4.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                if (strs4[0].Length > 20)
                {
                    strs4[0] = strs4[0].Substring(0, 17) + "...";
                }
                Console.WriteLine("{0}|{1}", strs4[0], strs4[1]);
            }
            //打开键值对文档,查找匹配的项
            Console.WriteLine("输入要查询的项");
            string s5 = Console.ReadLine();
            string[] lines5 = System.IO.File.ReadAllLines(@"...\配置.ini", Encoding.Default);
            string value = "";
            foreach(string line5 in lines5)
            {
                string[] strs5 = line5.Split('=');
                if (s5 == strs5[0])
                {
                    value = strs5[1];
                }
            }
            if (value == "")
            {
                Console.WriteLine("文档中没有匹配的项");
            }
            else
            {
                Console.WriteLine("查询的项值为{0}",value);
            }
            Console.ReadLine();
        }
    }
}
(二)枚举类型

namespace _14_枚举类型
{
    enum State {Online,Offline,Invisible};//限定值取值。
    class Program
    {
        static void Main(string[] args)
        {
            State S = State.Online;
            if(S == State.Online)
            {
                Console.WriteLine("在线");
            }
        }
    }
}
(三)数组

数组的属性及遍历

namespace _15_数组
{
    class Program
    {
        static void Main(string[] args)
        {
            //声明整型类型数组,实例化
            int[] a = { 10,20,30};
            //声明一个整型数组
            int[] A;
            //实例化数组A,数值类型默认值为0,字符类型默认为空格
            A = new int[5];
            //取得数组最大值 整型数组方法
            Console.WriteLine(a.Max());
            //取得数组长度,整型数组属性
            Console.WriteLine(a.Length);
            //遍历数组元素
            for (int i = 0; i < a.Length;i++ )
            {
                Console.WriteLine(a[i]);
            }
            Console.ReadKey();

        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值