C#基础之十三 字符串

前言

      字符串在刚刚基础计算机的时候就有所接触,后来学习VB的时候接触的还是挺多的,尤其是在做学生、机房项目的时候,现在在进行C#的学习,还是离不开,我觉得学习计算机就是在跟这些“字符串”打交道,现在学习的就又比当初的深入了,内容也更加丰富了,这么好的一个学习的机会摆在自己眼前,所以就抓住了这一次机会,进行了一些学习总结,和大家分享一下!

知识串烧

  • 字符串替换:
    • string Replace(string oldValue,string new value)将字符串中的出现old Value的地方替换成new value。如:名字替换
  • 取子字符串:
    • string Substring(int startIndex,int length ),取从位置startIndex开始长度为length的子字符串,如果子字符串的长度不足length,则报错。
    • bool Contains(string value)判断字符串中是否含有子串value
    • bool StartsWith(string value)判断字符串是否以value开始
    • bool EndsWith(string value)判断字符串是否以子串value结束
    • int IndexOf(string value):取子串value第一次出现的位置
    • int IndexOf(string value,int startIndex)
  • 示例一:
    • 判断字符串是否以某个字符串开始的:(bool类型一行为StartsWith)
static void Main(string[] args)
        {
            string str = "丫头不听话了!";
            bool result = str.StartsWith("丫头很");//判断字符串是否以某个字符串开始的
            if (result)
            {
                Console.WriteLine("有这个字符串");
            }
            else
            {
                Console.WriteLine("没有这个字符串");
            }
            Console.ReadKey();
        }
  • 示例二:判断字符串是否以某个字符串结束的(bool类型一行为EndsWith)
namespace _07判断字符串是否以某个字符串结束的
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "丫头不听话了!";
            bool result = str.EndsWith("很好");
            if(result)
            {
                Console.WriteLine("有这个字符串");
            }
            else
            {
                Console.WriteLine("没有这个字符串");
            }
            Console.ReadKey();
        }
    }
}
  • 示例三:判断字符串中是否含有**内容:(bool类型一行为Contains)
static void Main(string[] args)
        {
string name = "小杨很邪恶!";
bool result = name.Contains("你好");//判断这句话里边有没有“你好”这个字符串
            if (result)
            {
                Console.WriteLine("字符串包含这个字符串");
            }
            else
            {
                Console.WriteLine("字符串中不包含这个字符串");
            }
            Console.ReadKey();
        }
    }
}
  • 示例四:通过字符串显示出索引:
 static void Main(string[] args)
        {
            string str = "丫头又不听话了,我都受不了了!";
            int index = str.IndexOf("又");//如果这个字符串在这个字符串中,那么就会把这个字符串的索引显示出来(如:“又”,显示结果为2),如果不在这个字符串中,则找不到结果,返回值为-1
            Console.WriteLine(index);
            Console.ReadKey();
        }
  • 示例五:通过索引找字符串:
static void Main(string[] args)
        {
            string str = "abagdasfgdasgadsfgas";
            int index = str.IndexOf("a",2);//找一个字符串,从某个索引开始找,找到了就返回索引,找不到就返回-1(此示例返回值为2)
            Console.WriteLine(index);
            Console.ReadKey();
        }
  • 示例六:找最后一个字符的索引:(.LastIndexOf)
 static void Main(string[] args)
        { 
            string str = "今天天气很好,就是太热了,受不了了!";
            int index = str.LastIndexOf("了");//找最后一个字符的索引
            Console.WriteLine(index);
            Console.ReadKey();
        }
  • 示例七:在某个索引处插入字符串:(.Insert)
static void Main(string[] args)
        {
            string st = "哈哈,你又不听话!";
            st = st.Insert(8,"了");//在索引为8的字前面插入“了”
            Console.WriteLine(st);
            Console.ReadKey();
        }
  • 示例八:将字符串以相反的顺序输出:
static void Main(string[] args)
        {
            //练习1.接受用户输入的字符串,将其中的字符以与输入相反的顺序输出,如:“abc”-“cba”
            Console.WriteLine("请输入内容");
            string text = Console.ReadLine();
            for(int i=text.Length -1;i>=0;i--)
            {
                Console.Write(text[i]);
            }
            Console.ReadKey();
        }
  • 示例九:接受一句英文,将单词以反序输出:(整个的单词)
 static void Main(string[] args)
        {
 //接收用户输入的一句英文,将其中的单词以反序输出,如:I am xiaowang变成xiaowang am I
            Console.WriteLine("请输入一句话");
            string text = Console.ReadLine();
            string[] strs = text.Split(' ');
            string st = "";
            for(int i=strs.Length -1;i>=1;i--)
            {
                st += strs[i] + " ";//双引号中有空格,为了以反序输出的时候,字符串中间也有空格,否则的话,会连在一起
            }
            Console.WriteLine(st+strs[0]);
            Console.ReadKey();
 static void Main(string[] args)
        {
//从Email中提取出用户名和域名,如:laosong@163.com
            string email = "[email protected]";
            string[] sts = email.Split('@');
            Console.WriteLine("用户名是{0}",sts[0]);
            Console.WriteLine("域名是{0}",sts[1]);
            Console.ReadKey();
  • 示例十一:用.Join将字符串用各种符号连接起来(示例是用“|”连接的):
 static void Main(string[] args)
        {
//将{“小狗”,“小猫”,“小猪”,“小兔”,“小猴”,“小马”,“小羊”}变成小狗|小猫|小猪|小兔|小猴|小马|小羊,然后在把|切割掉(在字符串之间加上|)
            string[] names = { "小狗","小猫","小猪","小兔","小猴","小马","小羊" };
            string st = string.Join("|",names);//想用什么符号连接起来,就把|改成什么符号
            Console.WriteLine(st);
            Console.ReadKey();
.IsNullOrEmpty:
 string str = null;
            bool result = string.IsNullOrEmpty(str);
            if(result)
            {
                Console.WriteLine("有东西");
            }
            else
            {
                Console.WriteLine("你猜");
            }
            Console.ReadKey();
  • 示例十二:删除字符串中指定位置的字符:
static void Main(string[] args)
        {
//删除此字符串中从指定位置到最后位置的所有字符
            string text = "丫头最近不听话了!";
            text = text.Remove(3,2);//从某个地方开始移除,移除多少个(此示例从索引为3的地方开始移除,移除两个,最后结果为“丫头最听话了!”,如果没有后面的2的话,就一直从索引为3的地方开始移除,一直到最后)
            Console.WriteLine(text);
            Console.ReadKey();
  • 示例十三:切掉句首句尾空格(但是不能切掉句子中间的空格):
static void Main(string[] args)
        {
string name = "  [email protected]  ";
            name = name.Trim();//切掉句首和句尾的空格(但是不能切掉句子中间的空格),还有两个,分别是TrimEnd,这是切掉后面的空格,TrimStart,这是切掉前面的空格
            Console.Write(name);
            Console.ReadKey();
  • 示例十四:让用户输入一句话,找出所有e的位置(找出相同内容的位置):
 static void Main(string[] args)
        {
            //让用户输入一句话,找出所有e的位置
            string st = "agewshgrwhrtdhrtjtygasdgrtnfgsgdfsgrettrhnrsfg";
            int count = 0;
            int index = st.IndexOf("e");
            while(index!=-1)//找不到(没有)的情况下,为-1
            {
                count++;
                Console.WriteLine("第{0}个e的索引为:{1}",count,index);
                index = st.IndexOf("e",index+1);
            }
            Console.WriteLine();
            Console.ReadKey();
        }

总结

      这一部分的内容还是挺多的,不是很容易掌握,需要一直的去实践,并且去总结,对C#这一部分的字符串又有了新的认识,跟之前的有所不同,有了很多的新的地方,对这一部分进行了总结,方便大家、方便自己,欢迎大家多多提出宝贵的建议和意见!!!

猜你喜欢

转载自blog.csdn.net/tigaobansongjiahuan8/article/details/81051661