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

本文展示了C#中处理字符串的一些常见方法,包括检查字符串是否以特定字符开头(startswith)、是否以特定字符结束(endswith)、查找子字符串位置(IndexOf)、拆分字符串(Split)、转换大小写、插入、替换和删除字符串部分、去除空格(Trim)以及字符串对比和填充(PadRight)等操作。
摘要由CSDN通过智能技术生成

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]);
                }
            }

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

// endswith  字符串以。。。结尾
            for (int i = 0; i < strArr.Length; i++)
            {
                if(strArr[i].EndsWith("d"))
                {
                    Console.WriteLine(strArr[i]);
                }
            }

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

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

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

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

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

string a = str.Substring(4);
            string b = str.Substring(4,6);
            Console.WriteLine(a);
            Console.WriteLine(b);

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

string[] sd = str.Split(' ');
            for (int i = 0; i < sd.Length; i++)
            {
                Console.WriteLine(sd[i]);
            }

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

// 字符串转大小写
            string az = "ASDasdfg";
            string q = az.ToUpper();
            Console.WriteLine(q);
            string w = az.ToLower();
            Console.WriteLine(w);

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);

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

string asd = "  qwe  ";
            string asa = asd.Trim();
            Console.WriteLine(asa);

10:字符串对比

(1):str1 == str2

string str1 = "123";
            string str2 = "234";
            if (str1 == str2)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }

(2):Equals

// Equals
            if (str1.Equals(str2))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }

(3):Equals

if (Equals(str1, str2))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }

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

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

测试使用全部代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 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]);
                }
            }
 
            // endswith  字符串以。。。结尾
            for (int i = 0; i < strArr.Length; i++)
            {
                if(strArr[i].EndsWith("d"))
                {
                    Console.WriteLine(strArr[i]);
                }
            }
 
            // IndexOf  查找第一次在字符串中出现的位置(字符串,找到哪里)
            string str = "hello world";
            int index = str.IndexOf("ll",str.Length);
            Console.WriteLine(index);
 
            // LastIndexOf 查找字符串最后一次出现的位置(从后往前找)
            int ind = str.LastIndexOf("l",str.Length);
            Console.WriteLine(ind);
 
            // IndexOfAny 同时搜索多个字符串,直到找到其中一个位置
            char[] rest = { 'o', 'l' };
            int xx = str.IndexOfAny(rest);
            Console.WriteLine(xx);
 
            // 截取字符串substring (从那开始,截取几位)
            string a = str.Substring(4);
            string b = str.Substring(4,6);
            Console.WriteLine(a);
            Console.WriteLine(b);
 
            // 拆分字符串 split
            string[] sd = str.Split(' ');
            for (int i = 0; i < sd.Length; i++)
            {
                Console.WriteLine(sd[i]);
            }
 
            // 字符串转大小写
            string az = "ASDasdfg";
            string q = az.ToUpper();
            Console.WriteLine(q);
 
            string w = az.ToLower();
            Console.WriteLine(w);
 
            // 修改字符串 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);
 
            // Trim 去除字符串空格
            string asd = "  qwe  ";
            string asa = asd.Trim();
            Console.WriteLine(asa);
 
            // 字符串对比
            // ==
            string str1 = "123";
            string str2 = "234";
            if (str1 == str2)
            {
                Console.WriteLine("true");
            }
            else
            {
                Console.WriteLine("false");
            }
            // Equals
            if (str1.Equals(str2))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }
            // Equals
            if (Equals(str1, str2))
            {
                Console.WriteLine(true);
            }
            else
            {
                Console.WriteLine(false);
            }
 
            // padLeft
            string str3 = "中偶人";
            string str4 = str3.PadRight(10,'2');
            Console.WriteLine(str4);
 
            Console.ReadKey();
        }
    }
}

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

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值