C#——String类
string类型
构造函数
public string(char[] value)
public string(char[] value,int offset,int count)
拼接字符串: + or +=
比较字符串
public static int Compare(string strA,string strB)
public static int Compare(string strA,string strB,bool ignoreCase) //ignoreCase 是否忽略大小写
public int CompareTo(Object value)
public int CompareTo(string value) str1.CompareTo(str2);
public bool Equals(string value) str1.Equals(str2);
public static bool Equals(string a,string b) string.Equals(str1,str2);
格式化字符串
public static string Format(string format,Object argo) public static string Format(string format,params Object[] args) Console.WriteLine(string.Format("{0}{1}{2}{3}",a,A,67,true)); //还有一种简化写法,用“$”代替 string.Format () Console.WriteLine($"{a}{A}{67}{true}"); Console.WriteLine("{0}{1}{2}{3}",a,A,67,true);
string.Format("货币格式:{0:C}",100); // ¥100.00
string.Format("十进制格式:{0:D}",100); //100
string.Format("指数格式:{0:E3}",100000); //1.000E+005
string.Format("固定精度格式:{0:F2}",Math.PI); //3.14
string.Format("逗号分隔千位:{0:N}",12345678);//12,345,678.00
string.Format("百分比格式:{0:P}",0.5); //50.00%
string.Format("十六进制格式:{0:X}",20); //14
DateTime Date = DateTime.Now; Console.WriteLine(string.Format("短日期格式:{0:d}",Date));
Console.WriteLine(string.Format("长日期格式:{0:D}",Date));
Console.WriteLine(string.Format("完整日期时间格式:{0:f}",Date));
Console.WriteLine(string.Format("短时间格式:{0:t}",Date));
Console.WriteLine(string.Format("长时间格式:{0:T}",Date));
Console.WriteLine(string.Format("月日格式:{0:M}",Date));
Console.WriteLine(string.Format("年月格式:{0:Y}",Date));
/*输出
短日期格式:6/18/2021
长日期格式:Friday, June 18, 2021
完整日期时间格式:Friday, June 18, 2021 8:49 AM
短时间格式:8:49 AM
长时间格式:8:49:16 AM
月日格式:June 18
年月格式:June 2021
*/
格式化的另一种方法: Date.ToString("D"); i.ToString("C");
截取字符串
public string Substring(int startIndex)
public string Substring(int StartIndex,int length) //length 要截取的字符数
//例.从身份证号中读取出生年月日
string str = "123456199001010000";
Console.WriteLine(str.Substring(6,8));
分隔字符串
public string[] Split(params char[] separator) //separator 指定分隔符号
public string[] Split(char[] separator,int count) //count 指定分隔次数
//例.分隔邮箱收件人地址
string str = "123456@qq.com;987654@qq.com;147258@qq.com";
string[] strArray = str.Split(new char[] {';'},2);
for (int i = 0;i<str.Length;i++)
{
Console.WriteLine(strArray[i]);
}
/*输出:
123456@qq.com
987654@qq.com;147258@qq.com
*/
插入字符串
public string Insert(int startIndex,string value)
str.Insert(5,"World");
删除字符串
public string Remove(int startIndex)
public string Remove(int startIndex,int count)
str.Remove(5,5);
复制字符串
public static string Copy(string str)
public void CopyTo(int sourceIndex,char[] destination,int destinationIndex,int count) //(起始位置,存储字符串数组,字符数组开始存储位置,复制字符个数)
string strOld = "This is a test.";
char[] strNew = new char[4];
strOld.CopyTo(strOld.IndexOf("test"),strNew,0,4); //IndexOf 查找字串中指定字符或字串首次出现的位置,返首索引值
替换字符串
public string Replace(char OChar,char NChar) //替换字符
public string Replace(string OValue,string NValue) //替换字符串
string str = "1个人1顿饭吃1个包子喝1碗粥。";
string str1 = str.Replace('1', '一');
string str2 = str.Replace("包子", "鸡蛋");
/*输出:
一个人一顿饭吃一个包子喝一碗粥。
1个人1顿饭吃1个鸡蛋喝1碗粥。
*/
可变字符串类
using System.Text;
public StringBuilder()
public StringBuilder(int capacify) //指定容量
public StringBuilder(string value) //指定初始值
public StringBuilder(int capacity,int maxCapacity) //起始大小,最大容量
public StringBuilder(string value,int capacity) //指定初始值和初始大小
public StringBuilder(string value,int startIndex,int length,int capacity) //指定value中的一部分创建,从startIndex开始,截取长度为length
//常用方法
strb.Insert(int startIndex,Object value);
strb.Remove(int startIndex,int length);
strb.Replace(char OldChar,char NewChar);
strb.Append(Object value);