【C#】基础——字符串

还是直接看代码吧!

using System;

namespace SString
{
    class Program
    {

        void UnderLine()
        {
            Console.WriteLine("-----------------------------------------------");
        }

        public void jichu()
        {
            //字符串+字符串链接
            string fname, lname;
            fname = "Rowan";
            lname = "Atkinson";
            string fullname = fname + lname;
            Console.WriteLine("Fullname : {0}", fullname);

            //通过使用string构造函数
            char[] letters = { 'H', 'e', 'l', 'l', 'o' };
            string greetings = new string(letters);
            Console.WriteLine("Greeting : {0}", greetings);

            //方法返回字符串
            string[] sarray = { "Hello", "From", "Tutorials", "Point" };
            string message = String.Join(" ", sarray);
            Console.WriteLine("Message : {0}", message);

            //用于转化值的格式化方法
            DateTime waiting = new DateTime(2012, 10, 10, 17, 58, 1);
            string chat = String.Format("Message sent at {0:t} on {0:D}", waiting);
            Console.WriteLine("Message : {0}", chat);
        }

        //字符串比较
        public void Compare()
        {
            string str1 = "This is text";
            string str2 = "This is text";

            if (String.Compare(str1, str2) == 0)
            {
                Console.WriteLine(str1 + " and" + str2 + " are equal");
            }
            else
            {
                Console.WriteLine(str1 + " and" + str2 + "are not equal");
            }
        }

        //判断一个字符串中是否包含某个子串
        void Contain()
        {
            string str = "This is text";
            if(str.Contains("text"))
            {
                Console.WriteLine("The Sequence 'text' was found");
            }
            else
            {
                Console.WriteLine("The Sequence 'text' was not found");
            }

            if (str.Contains("texts"))
                Console.WriteLine("The Sequence 'texts' was found");
            else
                Console.WriteLine("The Sequence 'texts' was not found");
        }

        //获取子串
        void GetSubStr()
        {
            string str = "Last night I dreamt of San Pedro";
            Console.WriteLine(str);
            //从第23个开始截取、截取到最后;
            string substr = str.Substring(23);
            Console.WriteLine(substr);
        }

        //链接一个字符串数组中的所有元素、使用指定的分隔符分割每个元素
        void Join()
        {
            string[] starray = new string[] {"Down the way nights are dark",
                "And the sun shines daily on the mountain top",
                "I took a trip on a sailing ship",
                "And when I reached Jamaica",
                "I made a stop"
            };
            //链接一个字符串数组中的所有元素、使用指定的分隔符分割每个元素
            string str = String.Join("\n", starray);
            Console.WriteLine(str);
        }



        static void Main(string[] args)
        {

            Program P = new Program();
            P.jichu();
            P.UnderLine();

            P.Compare();
            P.UnderLine();

            P.Contain();
            P.UnderLine();

            P.GetSubStr();
            P.UnderLine();

            P.Join();
            P.UnderLine();

            Console.ReadKey();
         //   Console.WriteLine("Hello World!");
        }
    }
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值