using System;
using System.Text;
namespace 字符串方法
{
class Program
{
static void Main(string[] args)
{
//1.
string s = " wei715547";
int res = s.CompareTo("wei715547");//元素相等时,返回值为0,
//调用者字母排序靠后时返回值为1,靠前是返回值为-1
Console.WriteLine(res);
//2
string newstring = s.Replace('w', 'z');//替换字符
Console.WriteLine(newstring );
//string newstring2 = s.Replace('1', '2');
//3.字符串从指定字符位置拆分为数组
string[] stringArray = s.Split('1');
foreach (var item in stringArray )
{
Console.WriteLine(item );
}
string s = "abc-----[[[[####wei"
string sArray = str.Split(new char[]{'-','[','#'},StringSplitOption.RemoveEmptyEntries)//将字符数组中的元素置换为空,字符截取选项移除空项-返回字符串数组
//4.Substring.截取字符串
string newcut = s.Substring(2,5);
Console.WriteLine(newcut);
//5.清除字符串首尾空白
string clearspace = s.Trim();
Console.WriteLine(clearspace );
//6.返回指定字符的索引
int newindex = s.IndexOf('1');
Console.WriteLine(newindex );
//7.
//-------------------------------------------------------------
//可变字符串StringBuilder
//create1.通过构造函数进行构建
StringBuilder sb = new StringBuilder("www.wei7157.github.co");
Console.WriteLine(sb);
//create2.声明空间大小
StringBuilder sb2 = new StringBuilder(20);
sb2.Append("www.wei7157.github.co");
Console.WriteLine(sb2);
//creat3.赋值同时声明空间大小
StringBuilder sb3 = new StringBuilder("www.wei7157.github.co", 100);
Console.WriteLine(sb3);
//字符串修改与标准字符串对比
sb.Append("000000");//在空余空间链接字符
Console.WriteLine(sb);
(可用于追加Html代码实现网页内容)
//对比常规字符串操作,运行效率更高
string str = "www.wei7157.github.co";
str += "000000";//在堆中创建新的字符,str索引指向新的字符,原来的字符被CLR回收(效率低)
Console.WriteLine(str);
//可变字符串Insert()方法
sb.Insert(0, "http//");
Console.WriteLine(sb);
sb.Remove(0, 6);//根据索引范围移除字符
Console.WriteLine(sb);
sb.Replace('w', 'z');//字符替换
Console.WriteLine(sb);
Console.WriteLine(sb.ToString());//ToString()转变为不可变字符
}
}
}