C# String类常用的方法的使用

一、string类常用方法

    1)  IndexOf():查找某个字符在字符串中的位置;   如果没有,则返回-1;返回的第一次出现的位置      

<pre name="code" class="csharp">            Console.Write("请输入你的邮箱:");
            string email = Console.ReadLine();    // 电子邮件地址   
            Console.WriteLine("你的邮箱是 {0}", email);
            //zhangsan@qq.com
            int index = email.IndexOf("@");
            Console.WriteLine(index);

 


    2)LastIndexOf()  查找某个字符在字符串中的位置 :如果没有,则返回-1;返回的最好一次出现的位置   
            Console.Write("请输入你的邮箱:");
            string email = Console.ReadLine();    // 电子邮件地址   
            Console.WriteLine("你的邮箱是 {0}", email);
            //zhangsan@qq.com
            int index = email.LastIndexOf("@");
            Console.WriteLine(index);
 

   3)Substring(起始位置,截取字符个数):从字符串中截取子字符串

用于获取源字符串指定起始位置startindex,指定长度len的字符串。

参数Startindex索引从0开始,且最大值必须小于源字符串的长度,否则会编译异常;

参数len的值必须不大于源字符串索引指定位置开始,之后的字符串字符总长度,否则会出现异常

            Console.Write("请输入你的邮箱:");
            string email = Console.ReadLine();    // 电子邮件地址   
            Console.WriteLine("你的邮箱是 {0}", email);
            //string uname = email.Substring(3);  
            string uname = email.Substring(0, index);
            Console.WriteLine(uname);
    4)   string   Trim(  ); 去除左右空格

            string address = "    HangZhou XI HU    ";
            Console.WriteLine(address);
            string trims = address.Trim();
            Console.WriteLine(trims);
    5)转换成小写/大写

            string lower = address.ToLower();
            string upper = address.ToUpper();
            Console.WriteLine(lower+"\t"+upper);
    6)  == 和 Equals()有何区别

           //==: 常用来比较int、double等数值类型
            int num1 = 20;
            Console.WriteLine(num1==30);
            Console.WriteLine(num1 == 30.5);


            //Equals():常用来比较对象
            string name = "hello";
            Console.WriteLine(name.Equals("hello")+"\t"+(name=="hello"));  //true  true

              //判断空字符串的三种代码写法
             //name.Length == 0     //(1)
             //name == String.Empty   //(2)不占用存储空间
             //name == ""  //(3)占用存储空间

   7) Compare(string strA,string strB) 字符串比较

            string s1 = "animal";
            string s2 = "animals";
            //s1<s2  -1
            //s1=s2    0
            //s1>s2    1

            Console.WriteLine("Comparison of '{0}' and '{1}': {2}",
                              s1, s2, String.Compare(s1, s2));

  8)Split()方法:分割字符串

            string str = "one two three four five";
            Console.WriteLine(str);

           string[] arrs= str.Split(' ');
           foreach (string item in arrs)
           {
                Console.WriteLine(item);
           }

  9) string.Join("分隔符",数组)方法:连接字符串

<span style="white-space:pre">	</span>   string joins = string.Join("-", arrs);
           Console.WriteLine(joins);

  10)string.Format("格式",对应值)


           string names = "boy";
           int age = 20;
           Console.WriteLine("我的名字是 {0} ,我的年龄是{1} ", names, age);

           string mys = string.Format("我的名字是 {0} ,我的年龄是{1} ", names, age);
           Console.WriteLine(mys);

11)CompareTo()

 字符串对象也有一个CompareTo()方法,它根据某个字符串是否小于(<)或大于(>)另一个,返回一个整数值。

  比较字符串时使用Unicode值,小写的值小于大写的值。

<span style="white-space:pre">	</span>string sa1="ABC";  
<span style="white-space:pre">	</span>string sa2="abc";  
<span style="white-space:pre">	</span>if(sa1.CompareTo(sa2)>0)  
<span style="white-space:pre">	</span>{  
<span style="white-space:pre">		</span>Console.WriteLine("Greater-than");  
<span style="white-space:pre">	</span>}  
<span style="white-space:pre">	</span>else 
<span style="white-space:pre">	</span>{  
<span style="white-space:pre">		</span>Console.WriteLine("Less-than");  
<span style="white-space:pre">	</span>} 


12)Replace()替换字符

格式:Replace(stringoldValue,stringnewValue)

用于C#字符串中特定字符串组合的替换,即将源字符串中的所有oldValue字符串替换为newValue字符串。

<span style="white-space:pre">	</span>strings5="VisualC#Express";  
<span style="white-space:pre">	</span>Console.WriteLine(s5.Replace("C#","JAVA"));  //"VisualJAVAExpress" 


 13)ToString()   如同所有从Object派生的对象一样,字符串也提供了ToString方法,用于将值转换为字符串。此方法可用于将数值转换为C#字符串

  1. int year=2015;
  2. string msg="Evewasbornin"+year.ToString();
  3. Console.WriteLine(msg);

   总之,这些是C#string类常用的方法,方便大家参考学习哦!



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值