现在的位置: 首页 > 综合 > 正文

C#学习笔记二 字符、字符串类

2013年09月10日 ⁄ 综合 ⁄ 共 5163字 ⁄ 字号 评论关闭

1.char

Char类在C#中表示一个unicode字符。

注意Char类中的方法大都是static静态方法,直接用Char类调用;实例也有几个方法。示例如下:

char myChar = 'a';
string str1 = Char.ToString(myChar); //静态方法
Console.WriteLine(str1);
string str2 = myChar.ToString(); //实例方法
Console.WriteLine(str2);

 

2.string

String类表示由Unicode格式编码的一系列字符所表示的字符串。一旦创建了对象就不能再修改。

表面看起来可以修改字符串的所有方法其实都不是真的修改,实际上返回一个根据所调用方法修改的新的string对象。

如果需要修改String字符串的内容,可以使用StringBuilder类。

string str;

string str = "C#字符串";

2.1 字符串比较

(1)Compare

int Compare(string strA, string strB [,bool ignorCase]);

类的静态方法,Compare并非比较字符串的长度,而是比较字符串在英文字典中的位置。返回0表示相等

例:string.Compare(str1,str2,true);

(2)CompareTo

public int CompareTo(string strB);

实例对象本身与指定字符串比较

string str1="tjt-c#学习笔记";

string str2="tjt-c#学习方法";

int result = str1.CompareTo(str2);

(3)Equals 区分大小写

public bool Equals(string value); 实例调用

public static bool Equals(string a, string b); 类调用

该方法直接返回bool值,而非int值

 

2.2格式化字符串

String类提供了静态的Format方法,用于将字符串格式为指定的格式。

public static string Format(string format, object obj);

示例:

string strA="学习笔记";

string strB="天道酬勤";

string newStr=string.Format("{0},{1}",strA,strB);

 

日期时间的格式规范:

方法一:

DateTime dt = DateTime.Now;

string str=string.Format("{0:D}",dt);

结果: 2011年06月17日

方法二:

DateTime dt=new DateTime();

dt.ToString("yyyy-MM-dd");

 

2.3 截取字符串

public string Substring(int startIndex,int length);

 

2.4 分割字符串

public string[] Split(params char[] separator);

示例:

这两行

string[] splitStrings = new string[100];
splitStrings = strA.Split(separator);

也可以直接写成:

string[] splitStrings = strA.Split(separator);

 

2.5 插入和填充字符串

public string Insert(int startIndex, string value);

 

2.6 填充字符串,填充到指定长度

public string PadLeft(int totalWidth, char paddingChar);

public string PadRight(int totalWidth, char paddingChar);

 

2.7 删除字符串

public string Remove(int startIndex);

public string Remove(int startIndex,int count);

 

2.8 复制字符串

(1)copy

public static string Copy(string str); 静态方法

示例:

string strA = "学习ing";

string strB = string.Copy(strA);

(2)copyTo

public void CopyTo(int sourceIndex,char[] destination,int destination,int count);

把字符串的某一部分复制到一个字符数组中。

 

2.9 替换字符串

public string Replace(char OChar, char NChar);   字符替换

public string Replace(string OValue, string NValue);  字符串替换

 

 

3.StringBuilder

表示可变字符串,位于System.Text命名空间下。创建后可以进行追加、移除、替换、或插入字符操作。

容量可通过Capacity属性或EnsureCapacity方法来修改,不指定大小则为默认值。

六种构造函数:

public StringBuilder();

public StringBuilder(int capacity);

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);

参数说明:

capacity: 对象的建议起始大小

value:初始化对象的子字符串

maxCapacity:当前字符串可包含的最大字符数

startIndex:value中子字符串开始的位置

length:字符串中的字符数

方法:

Append:附加到尾部

AppendFormat:自定义变量的格式并将其值追加到尾部

Insert:插入

Remove:移除指定数量的字符

Replace:替换

示例:

 

4.String与StringBuilder的不同

String对象是不可改变的,每次使用String类的方法时,都会在内存中创建一个新的字符串对象,这就需要为该对象分配新的空间,在需要对字符串执行重复修改的情况下,开销会比较大。

 

字符串的连接用 +

strNew = "@@" + strOld;

Console.WriteLine("分行后" + strNew);

 

示例:

(1)反转字符串

 

(2)从绝对路径中提取出文件路径、文件名及扩展名

抱歉!评论已关闭.