• C#字符串


     static void Main(string[] args)
            {
                #region indexof//用法
                //string str = "今天天气";
                //int index= str.IndexOf("天",0);//从哪里开始找直到结尾
                //int sum = 0;
                //for (int i = 0; i < str.Length; i++)
                //{
                    
                //     sum=sum+i;
                //}
                //Console.WriteLine(sum);

                #endregion

    //indexof用法

                #region indexof 
                //string str = "今天天气";
                //int index= str.LastIndexOf("天");
               
                //string s= str.Substring(index);
                // Console.WriteLine(s);

                #endregion

    //replace用法

                #region replace替换
                //int count;
                //Console.WriteLine("输入要登记的学生数");
                //count = int.Parse(Console.ReadLine());
                //string[] names = new string[count];//开辟count个这么大的一个数组
                //for (int i = 1; i < count+1; i++)
                //{
                //    Console.WriteLine("请输入第{0}个学生的姓名", i);
                //    names[i] = Console.ReadLine();
                //}
                //Console.WriteLine("已登记的学生如下");
                //foreach (string name in names)//定义一个name在names数组中
                //{
                //    Console.WriteLine("{0}", name);
                //}
                //Console.ReadKey();

                #endregion

     

    //去掉空格Trim用法

                #region 去空格
               // string str = "    jgg   ";
               //string s= str.Trim();
               // Console.Write(s);
                
                #endregion
                #region 去空格
                //string str = null;
                //if (string.IsNullOrEmpty(str))
                //{
                //    Console.WriteLine("yes");
                //}
                


                #endregion

    //去空格用法2

                #region 去空格
               // string[] str = {"账单","les","王五","撒黄瓜"};
               //string s= String.Join("|", str);
               //Console.WriteLine(s);    

                #endregion

    //将字符串数组倒序循环

               #region 倒序循环
               //string str = "asd";
               //for (int i = str.Length-1; i >=0; i--)
               //{
               //    Console.Write(str[i]);
               //}

               #endregion

     

    //倒序循环字符串数组

                #region 倒序循环数组
                //string str = "sadfag";
                //char[] chs = str.ToCharArray();//字符串转成字符串数组
                //for (int i = 0; i < chs.Length/2; i++)//两两交换
                //{
                //    char temp=chs[i];//定义temp是第一个
                //         chs[i]=chs[chs.Length-1-i];//第一个跟最后一个换
                //         chs[chs.Length - 1 - i] = temp;//
                         
                //}
                //str = new string(chs);
                //Console.WriteLine(chs);

                #endregion

    //倒序循环数组中的字符串

                #region 倒序循环数组
                //string str = "hello c sharp";
                //string[] Newstr= str.Split(new char[]{' '},StringSplitOptions.RemoveEmptyEntries);//分割字符串 用空格
                //for (int i = 0; i < Newstr.Length/2; i++)
                //{
                //    string temp = Newstr[0];
                //    Newstr[0] = Newstr[Newstr.Length - 1 - i];//前面的跟后面的换
                //    Newstr[Newstr.Length - 1 - i] = temp;
                //}
                //for (int i = 0; i < Newstr.Length; i++)//遍历输出
                //{
                //    Console.WriteLine(Newstr[i]);
                //}

                #endregion

     

                #region 截取域名
                //string str = "1342077268@qq.com";


                //int index = str.IndexOf('@');//获取@的位置


                //string yh= str.Substring(0,index);//从0到index这个位置开始截取
                //string ym= str.Substring(index);//从index一直截取到最后


                //Console.WriteLine(yh);
                //Console.WriteLine(ym);


                //str.Substring(4,8);//截取从4到8
                //str.IndexOf('4');//找到4的位置
                #endregion
                #region 找出所有E的位置
                //string str = "dsgefedweefefsefee";
                //int index = str.IndexOf("e");//第一次出现得位置
                //Console.WriteLine("第一次出现的位置是{0}",index);
                //int count = 1;
                //while (index!=-1)//当索引不等于-1的时候
                //{
                //    count++;//计数
                //    index = str.IndexOf('e', index + 1);//找e从index+1的位置开始找返回一个数字
                //    if (index==-1)
                //    {
                //        break;//跳出循环
                //    }
                //    Console.WriteLine("第{0}次出现的位置是{1}",count,index);


                //}

                #endregion

    //replace用法,将邪恶换成**

                #region replace
                //string str = "老牛很邪恶";
                //if (str.Contains("邪恶"))
                //{
                //    str = str.Replace("邪恶","**");
                //}
                //Console.WriteLine(str);

                #endregion

     

    //用replace替换掉数组中的字符串

                #region replace
               // string[] names = {"诸葛","个个个","记大过","个"};
               // string str= string.Join("|",names);
               //string[] Newstr = str.Split(new char[]{'|'},StringSplitOptions.RemoveEmptyEntries);//用Split分割
               // Console.WriteLine(str);
               // //Console.WriteLine(Newstr.ToString());
               // Console.WriteLine();
                #endregion
                Console.ReadKey();
            }

    //如有不对请指正批评,因为本人正在学习,谢谢

  • 相关阅读:
    115今天太给力了~
    使用jQuery顺序显示元素
    温习浏览器渲染模式
    去除浏览器a标签链接时,烦人的虚线框
    设计可以是一种垄断
    如何获取鼠标选中的文字
    积极参与到FuckIE6的队伍中...
    google今儿发现页面又变好看了
    20101207google 今天出新功能了
    火狐ff下margintop太给力
  • 原文地址:https://www.cnblogs.com/yaodengfeng/p/7441048.html
Copyright © 2020-2023  润新知