string类(一、string基础)

string基础

1.字符串常量具备字符串池特性。
字符串常量在创建前,首先在字符串池中查找是否存在相同文本。
如果存在,则直接返回该对象引用;若不存在,则开辟空间存储。
目的:提高内存利用率。

2.字符串具有不可变性。
字符串常量一旦进入内存,就不得再次改变。
若在原位置改变会使其他对象内存被破坏,导致内存泄漏。
当遇到字符串变量引用新值时,会在内存中新建一个字符串,将该字符串地址交由该变量引用。

string str="";
for(int i=0;i<10;i++)
{
    str=str+i.ToString(); 
    //字符串的拼接:每次拼接都会产生一个新的对象替换原有引用,每次循环都会产生一个垃圾
}
Console.WriteLine(str);//0123456789

解决方案:
可变字符串StringBuilder:一次开辟可以容纳n个字符的空间

StringBuilder builder=new StringBuilder(10);
int length=builder.Length; //有效字符数量
for(int i=0;i<10;i++)
{   //每次循环都在原有对象上修改
    builder.Append(i);//使用builder追加字符串
}
Console.WriteLine(builder.ToString());

拓展:

StringBuilder类

1.构造函数
1/ 无参构造 StringBuilder();
创建的对象没有内容,需要加入值才能打出来

2/ 传入容量 StringBuilder(int capacity);
使用指定的容量初始化对象,参数为建议起始大小

3/ 传入内容 StringBuilder(string value);
使用指定的字符串初始化 System.Text.StringBuilder 类的新实例。

StringBuilder sb=new StringBuilder("www.baidu.com");

4/ StringBuilder(int capacity, int maxCapacity);
初始化StringBuilder 类的新实例,该类起始于指定容量并且可增长到指定的最大容量。

5/ StringBuilder(string value, int capacity);
使用指定的字符串和容量初始化 StringBuilder 类的新实例。

6/ StringBuilder(string value, int startIndex, int length, int capacity);
用指定的子字符串和容量初始化StringBuilder 类的新实例。

value: 字符串,包含用于初始化此实例值的子字符串。
startIndex: value 中子字符串开始的位置。
length: 子字符串中的字符数。
capacity: StringBuilder 的建议起始大小。

2.属性
1/ Capacity: 获取或设置可包含在当前实例所分配的内存中的最大字符数。
2/ Length:获取或设置当前StringBuilder 对象的长度。(有效字符数量)
3/ MaxCapacity: 获取此实例的最大容量。

3.功能函数
1/ Append(…)追加值,有空位坐空位,没有则新加一个空位
…可以是bool,byte,char,char[],decimal,double,float,int,long,object,string….

2/ Append(char value,int repeatCount)
在此实例追加 Unicode 字符的字符串表示形式指定数目的副本。

3/ Append(char[] value, int startIndex, int charCount);
Append(string value, int startIndex, int count);
在此实例追加指定的 Unicode 字符子数组的字符串表示形式。

startIndex: value 中的起始位置
charCount: 要追加的字符数。

4/ AppendFormat略
5/ AppendLine()
将默认的行终止符追加到当前 System.Text.StringBuilder 对象的末尾。

6/ AppendLine(string value);
将后面跟有默认行终止符的指定字符串的副本追加到当前 StringBuilder 对象的末尾。

7/ Clear();
从当前 System.Text.StringBuilder 实例中移除所有字符。

8/ CopyTo(int sourceIndex, char[] destination, int destinationIndex, int count);
将此实例的指定段中的字符复制到目标Char数组的指定段中。

sourceIndex: 此实例中开始复制字符的位置。 索引是从零开始的。
destination: 将从中复制字符的数组。
destinationIndex: destination 中将从其开始复制字符的起始位置。 索引是从零开始的。
count: 要复制的字符数。

9/ EnsureCapacity(int capacity);
确保 System.Text.StringBuilder 的此实例的容量至少是指定值。
capacity: 要确保的最小容量。
返回结果: 此实例的新容量。(取两者的最大值)

StringBuilder sb = new StringBuilder(10);
Console.WriteLine(sb.EnsureCapacity(4)); //10
Console.WriteLine(sb.EnsureCapacity(20)); //20

10/ Equals(StringBuilder sb);
返回一个值,该值指示此实例是否等于指定的对象。
如果此实例和 sb 具有相等的字符串、System.Text.StringBuilder.Capacity 和 System.Text.StringBuilder.MaxCapacity值,则为 true;否则,为 false

11/ Insert(int index, …);
…为各种类型数据;将…的字符串表示形式插入到此实例中的指定字符位置。
index: 此实例中开始插入的位置。 …:要插入的值

12/ Remove(int startIndex, int length);
将指定范围的字符从此实例中移除。

13/ Replace(string oldValue, string newValue);
Replace(byte oldValue, byte newValue);
将此实例中所有指定字符串的匹配项替换为其他指定字符串。

14/ Replace(string oldValue, string newValue, int startIndex, int count);
将此实例的startIndex开始count计字符数的子字符串中所有指定字符串的匹配项替换为其他指定字符串。

15/ ToString();转换为字符串
ToString(int startIndex, int length);
将此实例中子字符串的值转换为 System.String。

原文地址:https://www.cnblogs.com/caymanlu/p/6361676.html