掌握这些C#的字符串运用技巧就很省事了

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Test
{
    public partial class Form1 : Form
    {

        [DllImport("kernel32.dll")]
        public static extern Boolean AllocConsole();
        [DllImport("kernel32.dll")]
        public static extern Boolean FreeConsole();



        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AllocConsole();
        }
        /// <summary>
        ///  1>把字符串变成字符数组,toCharArray()
        ///2>对字符数组进行修改
        ///3>把字符数组变成字符串,new string
        /// </summary>
        public void TestFunc()
        {
            string text = "我喜欢你";
            char[] chs = text.ToCharArray(); //把字符串变成字符数组
            chs[1] = '爱';
            chs[2] = 'o';
            text = new string(chs); //把字符数组变成字符串
            MessageBox.Show(text); //显示为我爱o你

        }
        public void TestFunc1()
        {
            string text = "hello";
            text = "world";
            MessageBox.Show(text);
        }

        /// <summary>
        ///   Upper:将字母转化为大写

        /// Lower:将字母转化为小写

        /// Equals:忽略字母的大小写
        /// </summary>
        public void TestFunc2()
        {
            string Str = "ABCDefgh";
            string Str1 = Str.ToUpper(); //字符变大写
            MessageBox.Show(Str1); //结果为ABCDEFGH

            string Str2 = Str.ToLower(); //字符变小写
            MessageBox.Show(Str2);//结果为abcdegfh
            
            if(Str1.Equals(Str2,StringComparison.OrdinalIgnoreCase))//比较两个字符 忽略大小写
            {
                MessageBox.Show("除了大小写都一样");
            }
     
        }

        /// <summary>
        /// Split:分割字符串

        /// Replace:替换某个字符串

        /// Countain:判断某字符串中是否包含某字符串
        /// </summary>
        public void TestFunc3()
        {
            string text = "好好学习,天天向上";
            bool isContain = text.Contains("好好学习");//判断字符串是否包含该字符串
            if(isContain)
            {
                //返回包含此字符串中的子字符串(由指定的 Char 数组的元素分隔)的 String 数组。参数指定是否返回空数组元素。
                //StringSplitOptions.RemoveEmptyEntries不返回空数组元素
                string[] str = text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if(str[0].Contains("学习"))
                {
                    str[0]= str[0].Replace("学习", "打机"); //把学习替换成打机
                    MessageBox.Show(string.Format("{0}----{1}", str[0], str[1]));  //格式化字符串
                    //展示为好好打机,天天向上
                }
                return; 
            }
            else
            {
                MessageBox.Show(text);
                return;
            }
        }
        public void TestFunc4()
        {
            string text = "aatbbcct   tddtfft";
            string[] str = text.Split(new char[] { 't' }, StringSplitOptions.None); //包含空数组元素
            for(int i=0; i<str.Length;i++)
            {
                Console.Write(str[i]);//写元素没有换行
            }
            //展示为aabbcc   ddff

        }

        /// <summary>
        ///  判断某个字符串是否以某个字符串开始(startwith)或以某个字符串结束(endwith)
        /// </summary>
        public void TestFunc5()
        {
            string text = "天气真清新";
            bool result = text.EndsWith("清新");
            if(result)
            {
                Console.WriteLine("匹配字符串的尾部");
            }
            else
            {
                Console.WriteLine("不匹配字符串的尾部");
            }

            string text1 = "今天你笑了吗";
            bool result1 = text1.StartsWith("今天你");
            if(result)
            {
                Console.WriteLine("匹配字符串的前缀");
            }
            else
            {
                Console.WriteLine("不匹配字符串的前缀");
            }
        }

        /// <summary>
        /// 判断某个字符在字符串中的索引
        /// </summary>
        public void TestFunc6()
        {
            //从前面开始找的
            string path = "天天练习打代码";
            int index = path.IndexOf("练习");
            Console.WriteLine(index.ToString()); //2

            index = path.IndexOf("练习",3, 2);   //从前向后"练习" 定位  从第3 位开始查,查2位,即 从第3位到第5位;
            Console.WriteLine(index.ToString()); //-1 

            string path1 = "hello你好hello你好hello";
            index = path1.IndexOf("hello", 3);   //从第三位开始查,第一个出现的位置
            Console.WriteLine(index.ToString()); //7

            //从后面开始找到的一个
            string lastpath = "天天练习打代码,天天写博客,天天看博客";
            int lastindex = lastpath.LastIndexOf("天天");
            Console.WriteLine(lastindex.ToString());//14

        }

        /// <summary>
        /// substring的用法:截取字符串中的一段字符
        /// </summary>
        public void TestFunc7()
        {
            string path = @"c:\User\Gentle\Desktop\test.txt"; //@用上后,代表后面的'\'不带转义的作用
            int order = path.LastIndexOf("\\");//找到最后一个\的位置
            string name = path.Substring(order + 1); //order+1的位置截取到字符串结尾
            Console.WriteLine("截取到文件名为:{0}", name);//test.txt

            string text = "abcdefgh";
            string str = text.Substring(4, 2); //4是对应的下标开始,截取2位
            Console.WriteLine("截取到的文本为:{0}", str); //ef

        }

        /// <summary>
        /// insert的用法:在字符串的某个索引前插入值
        /// </summary>
        public void TestFunc8()
        {
            string text = "我是一名程序员";
            text = text.Insert(4, "菜鸟");
            Console.WriteLine(text);//我是一名菜鸟程序员
        }

        /// <summary>
        ///  Insert与join的区别:Join是在字符串数组的前提下操作的,在每个数组元素中间插入内容,而Inset是对字符串操 作,在某个索引前插入内容;
        /// </summary>
        public void TestFunc9()
        {
            string[] text = { "鼠标", "键盘", "显示屏" };
           string str = string.Join("|", text);   //是静态函数
            Console.WriteLine(str);
        }

        /// <summary>
        /// string.IsNullOrEmpty()的用法:判断该字符串是否为null或string.Empty
        /// NULL:不分配内存,无对象
        /// "":分配内存空间,有对象
        /// </summary>
        public void TestFunc10()
        {
            string text = string.Empty;
            bool isNull = string.IsNullOrEmpty(text);//为静态方法
            if (isNull)
            {
                Console.WriteLine("没有信息"); //输出这一行
            }
            else
            {
                Console.WriteLine("信息为:{0}", text);
            }

        }

        /// <summary>
        /// Trim是去掉字符串两端的空格
        /// TrimEnd是去掉字符串最后面的空格
        /// TrimStart是去掉字符串最前面的空格
        /// </summary>
        public void TestFunc11()
        {
            string text = "   你好吗   ";
            text = text.Trim(); //"你好吗"
            Console.WriteLine(text); //

            string text2 = "   新年快乐  ";
            text2 = text2.TrimEnd(); //"  新年快乐"
            Console.WriteLine(text2);

            string text3 = "  点个赞   ";
            text3 = text3.TrimStart(); //"点个赞   "
            Console.WriteLine(text3);


        }

        /// <summary>
        /// Format 的用法,信息量有点大先简单说一下
        /// </summary>
        public void TestFunc12()
        {
            string str1 = "hello";
            string str2 = "world";
            Console.WriteLine(string.Format("{0},{1}", str1, str2));//它是一个静态函数,格式化合并字符串
             
            //格式化日期
            string Date1 = string.Format("{0:d}", System.DateTime.Now); //结果为:2021/5/13
            Console.WriteLine(Date1);
            string Date2 = string.Format("{0:D}", System.DateTime.Now); //结果为:2021年5月13日
            Console.WriteLine(Date2);
            string Date3 = string.Format("{0:f}", System.DateTime.Now); //结果为:2021年5月13日 17:06
            Console.WriteLine(Date3);
            string Date4 = string.Format("{0:F}", System.DateTime.Now); //结果为:2021年5月13日 17:06:27
            Console.WriteLine(Date4);
            string Date5 = string.Format("{0:g}", System.DateTime.Now); //结果为:2021/5/13 17:06
            Console.WriteLine(Date5);
            string Date6 = string.Format("{0:G}", System.DateTime.Now); //结果为:2021/5/13 17:06:27
            Console.WriteLine(Date6);
            string Date7 = string.Format("{0:m}", System.DateTime.Now); //结果为:5月13日
            Console.WriteLine(Date7);
            string Date8 = string.Format("{0:t}", System.DateTime.Now); //结果为:17:06
            Console.WriteLine(Date8);
            string Date9 = string.Format("{0:T}", System.DateTime.Now); //结果为:17:06:27
            Console.WriteLine(Date9);





        }

        private void button1_Click(object sender, EventArgs e)
        {
            TestFunc12();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

困了就喝白茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值