C#基础03

1、字符串

字符串可认为是个字符数组,

但实际上是一个对象

using System;


namespace Demo
{
    class Program
    {
        static void Main()
        {
            string str = "1kc02e3c2de43t";
            string str2 = "1kc02e3c2de43t";
            string str3 = new String(new char[] { 'a','b','c'});
            // 判断两个数组的引用是否等同
            Console.WriteLine(object.ReferenceEquals(str,str2));
            Console.WriteLine(object.ReferenceEquals(str, str3));

            foreach (char c in str)
            {
                Console.WriteLine(c);
            }
            Console.Read();
        }
    }
}

 

using System;


namespace Demo
{
    class Program
    {
        static void Main()
        {
            string str = "1kc02e3c2de43t";
            string str2 = "1kc02e3c2de43t";
            string str3 = new String(new char[] { 'a','b','c'});
            // 判断两个数组是否等同
            Console.WriteLine(object.ReferenceEquals(str,str2));// true
            Console.WriteLine(object.ReferenceEquals(str, str2));// true
            Console.WriteLine(str==str2); // True
            Console.WriteLine(str == str3); // True

            foreach (char c in str)
            {
                Console.WriteLine(c);
            }
            Console.Read();
        }
    }
}

 

在C#中所有的基本类型都可以通过==来判断是否相同。

在C#中判断是否为同一对象使用object.ReferenceEquals()

操作不会对原始字符串有影响:

转大写:.ToUpper()

转小写:.ToLower()

字符串截取:.Substring(startIndex[,length])

字符查找,返回索引.(last)IndexOf(val[,startIndex,searchLength]) 从startIndex开始找searchLength长度为搜索范围,找到就返回

是否包含:.Contains()

 字符串分割:.Split('val',StringSplitOptions)

using System;


namespace Demo
{
    class Program
    {
        static void Main()
        {
            string str = "2e,4r,2d,3e,5t,6e,4c,7h,,,,,,8i,7p";
            string[] _str = str.Split(',');
            string[] _str2 = str.Split(new char[]{','},StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in _str)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine("-------------");
            foreach (var item in _str2)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

字符串拼接 +  、 StringBuilder

using System;
using System.Text;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("123");
            sb.Append("456");
            Console.WriteLine(sb);
            Console.Read();
        }
    }
}

占位符拼接

using System;
using System.Text;

namespace Demo
{
    class Program
    {
        static void Main()
        {
            string str = string.Format("姓名:{0} 年龄:{1}","Jessica",12);

            string name = "Bob";
            int age = 22;
            string str2 = $"姓名:{name} 年龄:{age}";

            Console.WriteLine(str);
            Console.WriteLine(str2);

            Console.Read();
        }
    }
}

 

2、枚举

using System;
using System.Text;

namespace Demo
{
    enum Sex
    {
        male,female
    }
    class Program
    {
        static void Main()
        {
            Sex boy = Sex.male;
            Console.WriteLine(boy);
            //字符串转枚举
            Console.WriteLine(Enum.Parse(typeof(Sex), "male"));
            Console.WriteLine((Sex)1);
            Console.ReadLine();
        }
    }
}

 

posted on 2020-02-24 11:21  Tanqurey  阅读(119)  评论(0编辑  收藏  举报