• C#(三十九)之关于string的一些函数


    1:startswith 字符串以。。。开头

    // startswith  字符串以。。。开头
                string[] strArr = { "asd","azx","qwe","aser","asdfgh"};
                for (int i = 0; i < strArr.Length; i++)
                {
                    if(strArr[i].StartsWith("as"))
                    {
                        Console.WriteLine(strArr[i]);
                    }
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    2:endswith 字符串以。。。结尾

    // endswith  字符串以。。。结尾
                for (int i = 0; i < strArr.Length; i++)
                {
                    if(strArr[i].EndsWith("d"))
                    {
                        Console.WriteLine(strArr[i]);
                    }
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    3:IndexOf 查找第一次在字符串中出现的位置(字符串,找到那)如果找不到,返回-1

    // IndexOf  查找第一次在字符串中出现的位置(字符串,从哪里开始找)
                string str = "hello world";
                int index = str.IndexOf("ll",0);
                Console.WriteLine(index);
    
    • 1
    • 2
    • 3
    • 4

    4:IndexOfAny 同时搜索多个字符串,直到找到其中一个位置

    char[] rest = { 'o', 'l' };
                int xx = str.IndexOfAny(rest);
                Console.WriteLine(xx);
    
    • 1
    • 2
    • 3

    5:截取字符串substring (从那开始,截取几位)

    string a = str.Substring(4);
                string b = str.Substring(4,6);
                Console.WriteLine(a);
                Console.WriteLine(b);
    
    • 1
    • 2
    • 3
    • 4

    6:拆分字符串 split 变成数组

    string[] sd = str.Split(' ');
                for (int i = 0; i < sd.Length; i++)
                {
                    Console.WriteLine(sd[i]);
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7:ToUpper/ToLower 字符串转为大写

    // 字符串转大小写
                string az = "ASDasdfg";
                string q = az.ToUpper();
                Console.WriteLine(q);
                string w = az.ToLower();
                Console.WriteLine(w);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    8:修改字符串 insert replace remove

    Insert(从第几位插入,插入元素);

    Replace(替换元素,换成啥)

    Remove(从第几位开始,删几位)

    string kk = "I Love You";
                string sx = kk.Insert(3,"s");
                Console.WriteLine(sx);
                string sc = kk.Replace("s"," ");
                Console.WriteLine(sc);
                string sv = kk.Remove(2,4);
                Console.WriteLine(sv);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    9:Trim 去除两端字符串空格(TrimEnd,TrimStart)

    string asd = "  qwe  ";
                string asa = asd.Trim();
                Console.WriteLine(asa);
    
    • 1
    • 2
    • 3

    10:字符串对比

    (1):str1 == str2

    string str1 = "123";
                string str2 = "234";
                if (str1 == str2)
                {
                    Console.WriteLine("true");
                }
                else
                {
                    Console.WriteLine("false");
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    (2):Equals

    // Equals
                if (str1.Equals(str2))
                {
                    Console.WriteLine(true);
                }
                else
                {
                    Console.WriteLine(false);
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    (3):Equals

    if (Equals(str1, str2))
                {
                    Console.WriteLine(true);
                }
                else
                {
                    Console.WriteLine(false);
                }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    11:PadLeft()、 PadRight() 在字串左(或右)加空格或指定char字符,使字串达到指定长度。(字符串的长度,以什么字符补充)

    string str3 = "中偶人";
    string str4 = str3.PadRight(10,'2');
    Console.WriteLine(str4);
    
    • 1
    • 2
    • 3

    有好的建议,请在下方输入你的评论。

    欢迎访问个人博客
    https://guanchao.site

    欢迎访问小程序:
    在这里插入图片描述

  • 相关阅读:
    PHP-学习笔记-部署wordpress
    JS中的 typeof 针对各种类型的返回值 以及typeof历史遗留问题
    分布式系统幂等解决方案
    自定义表单开源好用吗?有哪些优势特点?
    RT-Thread基于STM32H743的网络通信调试
    分数限制下,选好专业还是选好学校?
    单细胞流程 安装conda 下载基因组数据 然后走cellranger 流程得到10x数据
    虹科示波器 | 汽车免拆检修 | 2012 款上汽大众帕萨特车 发动机偶尔无法起动
    SpringBoot+ThreadPoolTaskExecutor+mybatis-plus 多线程批量插入大数量级数据
    Vue组件自定义事件父子
  • 原文地址:https://blog.csdn.net/qq_39708228/article/details/125895061