(第三季)101-字符串类string基础使用102-字符串类string更多的方法103-字符串类StringBuilder的构造 104-string和StringBuilder的区别105-St

转自siki

System.String类

1,创建字符串  string s = "www.devsiki.com";

2,获取字符串长度  s.Length(属性)

3,比较字符串是否一样s=="www.devsiki.com"

4,字符串连接  s="http://"+s;

5,使用类似索引器的语法来取得字符串中的某个字符stringName[index]  s[0] s[3]

关于string字符串:string创建的字符串实际上是一个不可变的数据类型,一旦对字符串对象进行了初始化,该字符串就不能改变内容了,上面的示例中实际上是创建了一个新的字符串,把旧字符串的内容复制到新字符串中。然后把新字符串的引用赋值为字符串的对象。(重复修改给定的字符串,效率会很低)

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

//
using System.Text.RegularExpressions;

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            string s = "I am blue cat.";
            int length = s.Length;
            char c = s[2];
            Console.WriteLine(c);

            Console.ReadKey();
        }
    }
}

关于字符串的更多方法

1,CompareTo()方法,比较字符串的内容

2,Replace()用另一个字符或者字符串替换字符串中给定的字符或者字符串

3,Split()在出现给定字符的地方,把字符串拆分称一个字符串数组

4,SubString()在字符串中检索给定位置的子字符串

5,ToLower()把字符串转换成小写形式

6,ToUpper()把字符串转换成大写形式

7,Trim()删除首尾的空白

8,Concat()方法,合并字符串

9,CopyTo()方法,把字符串中指定的字符复制到一个数组中

10,Format()方法,格式化字符串

11,IndexOf()方法,取得字符串第一次出现某个给定字符串或者字符的位置

12,IndexOfAny()方法,

13,Insert()把一个字符串实例插入到另一个字符串实例的制定索引处

14,Join()合并字符串数组,创建一个新字符串

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

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            string s = "taotao";
            // 当两个字符串相等的时候,返回0; 当s在字母表中的排序靠前的时候,返回-1,反之靠后

            int res = s.CompareTo("zaotao");
            Console.WriteLine(res);

            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//
using System.Text.RegularExpressions;

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            string s = "www.taotaoahui.com";
             当两个字符串相等的时候,返回0; 当s在字母表中的排序靠前的时候,返回-1,反之靠后
            //int res = s.CompareTo("zaotao");
            //Console.WriteLine(res);

            string newStr = s.Replace('.', '-');
            Console.WriteLine(s);
            Console.WriteLine(newStr);

            Console.ReadKey();
        }
    }
}



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

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            string s = "www.taotaoahui.com";
             当两个字符串相等的时候,返回0; 当s在字母表中的排序靠前的时候,返回-1,反之靠后
            //int res = s.CompareTo("zaotao");
            //Console.WriteLine(res);

            //string newStr = s.Replace(".", "-----");//把指定 的字符换成指定的字符,或者把指定的字符串换成指定的字符串
            //Console.WriteLine(s);
            //Console.WriteLine(newStr);

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

            //字符串截取
            string str = s.Substring(4, 10);
            string str1 = s.Substring(4); 

            Console.WriteLine(str);
            Console.WriteLine(str1);

            string str2 = (" sd  dsfsdf      ").Trim(); // 删除首尾空白
            Console.WriteLine(str2);

            // 用该方法判断当前字符串是否包含一个子字符串,若不包含返回-1,若包含,会返回第一个字符出现时的索引

            int length = ("taoahui").IndexOf("ahui");
            Console.WriteLine(length);


            Console.ReadKey();
        }
    }
}

103-字符串类StringBuilder的构造

StringBuilder类(位于System.Text命名空间下)

1,创建StringBuilder对象

  StringBuilder sb = newStringBuilder("www.taikr.com");

  StringBuilder sb = new StringBuilder(20);

  StringBuilder sb = newStringBuilder("www.devsiki.com",100);

  关于StringBuilder对象创建的时候的内存占用

2,Append()方法,给当前字符串追加一个字符

3,Insert()追加特定格式的字符串

4,Remove()从当前字符串中删除字符

5,Replace()在当前字符串中,用某个字符或者字符串全部替换另一个字符或者字符串

6,ToString()把当前stringBuilder中存储的字符串,提取成一个不可变的字符串


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


namespace taotao
{
    class Class1
    {
        static void Main()
        {
            // 1
            //StringBuilder sb = new StringBuilder("www.devsiki.com");
            // 2 初始一个空的stringbuilder对象,占有20个字符的大小
            //StringBuilder sb = new StringBuilder(20);
            //
            StringBuilder sb = new StringBuilder("www.taotaoahiu", 100);
            //string StringBuilder   string里面的内容是不可变的,
            //StringBuilder里面的内容是可变动的
            // 当字符串里面的字符是频繁变动的时候,我们最好用StringBuilder

            Console.ReadKey();
        }
    }
}

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

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            // 1 字符串连接
            //StringBuilder str = new StringBuilder("hello", 100);
            //str.Append("World!");
            //Console.WriteLine(str.ToString());

            // 2 字符串连接
            string s = "helloworld";
            s = s + "taotao";
            Console.WriteLine(s);

            /*
                string 比较慢,因为它要创建一块新的内存
                对于StringBuilder 当我们需要对一个字符串进行频繁的删除添加操作的时候,、
                使用StringBuilder的效率比较高
            */
            //
            Console.ReadKey();
        }
    }
}
StringBuilder类(位于System.Text命名空间下)



1,创建StringBuilder对象

  StringBuilder sb = newStringBuilder("www.taikr.com");

  StringBuilder sb = new StringBuilder(20);

  StringBuilder sb = newStringBuilder("www.devsiki.com",100);

  关于StringBuilder对象创建的时候的内存占用

2,Append()方法,给当前字符串追加一个字符

3,Insert()追加特定格式的字符串

4,Remove()从当前字符串中删除字符

5,Replace()在当前字符串中,用某个字符或者字符串全部替换另一个字符或者字符串

6,ToString()把当前stringBuilder中存储的字符串,提取成一个不可变的字符串


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

namespace taotao
{
    class Class1
    {
        static void Main()
        {
            // 1 字符串连接
            //StringBuilder str = new StringBuilder("hello", 100);
            //str.Append("World!");
            //Console.WriteLine(str.ToString());

            // 2 字符串连接
            //string s = "helloworld";
            //s = s + "taotao";
            //Console.WriteLine(s);

            /*
                string 比较慢,因为它要创建一块新的内存
                对于StringBuilder 当我们需要对一个字符串进行频繁的删除添加操作的时候,、
                使用StringBuilder的效率比较高
            */
            StringBuilder str = new StringBuilder("hello", 100);
            //str.Insert(0, "ahui");
            Console.WriteLine(str);

            //str.Remove(0, 3);
            //Console.WriteLine(str);

            str.Replace("e", "a");
            Console.WriteLine(str);



            //
            Console.ReadKey();
        }
    }
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值