c#-高级篇知识合集

001-string与StringBuilder

string在初始化之后其内容是不可变得,在对string进行增删的时,会在堆内存中开辟新空间保存新字符串并返回指向该字符串的引用。
StringBuilder与c++中的容器相似,具有自动扩充容量的功能,其内容是可变的。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _001_字符串string
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            string s = "www.baidu.com   "; //字符串需要加双括号
            int length = s.Length;
            Console.WriteLine(length);
            if (s == "xxxx")
            {
   
                Console.WriteLine("相同");
            }
            else
            {
   
                Console.WriteLine("不相同");

            }
            s = "http:\\\\" + s;//s的应用地址改变了,原字符串失去引用会被回收,字符串被创建后是不能修改的
            Console.WriteLine(s);
            char c = s[4];
            Console.WriteLine(c);
            for (int i = 0; i < length; i++)
            {
   
                Console.WriteLine(s[i]);
            }

            string str = "www.siki.com";
            int res = str.CompareTo("saki");//按照字典顺序比较,若str相对靠前则返回-1,若相同则返回0,若str相对靠后返回1;
            Console.WriteLine(res);
            String s2 = str.Replace('.', '-');//返回将‘.’替换成‘-’后的新字符串,原字符串str没有改变。
            String s3 = str.Replace(".", "----");//将“.”,替换为“----”
            Console.WriteLine(s2);
            Console.WriteLine(s3);

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

            }
            string subs = s.Substring(1,2);//“ww”
            string trimString = s.Trim();//去掉字符串的首尾空格。
            int index = s.IndexOf("baidu1");//返回子串首字母在原串中的索引,若子串不是原字符串的子串,那么返回值是0;
            Console.WriteLine(subs);
            Console.
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值