字符串

it2025-04-20  4

char结构在C#中是Unicode字符

比较重要的方法:

IsDigit                    指定某个Unicode字符是否属于十进制数字类别IsLetter                  是否输入字母类别IsLower                  是否输入小写字符类别IsNumber               是否输入数字IsPunctuation         是否属于标点符号类别IsSeparator            是否输入分隔符类型IsUpper                 是否输入大写字母IsWhiteSpace         是否属于空白类型ToLower                转化为小写ToString                转化为字符串表示ToUpper                 转化为大写

转义字符

\n      回车换行\t      横向跳到下一个制表位置\v      竖向跳格\b     退格\r      回车\f     换页\\    反斜线\'    单引号符

string类由一系列的Unicode格式编码的字符组成,使用string类时,一旦创建了对象,就不能够修改。

实际使用string类时,表面看来是能够修改字符串的所有方法实际上并不能修改,他们实际上返回一个根据调用的方法修改的新的string对象,如果需要修改字符串的实际内容,可以使用stringbuilder类

string类常用的一些方法

compare方法

常用的两种方法

public static int Compare(string strA,string strB);

pbulic static int Compare(string strA,string strB,bool ignoreCase);

ignoreCase 如果这个值是true,那么比较字符串的时候就忽略大小写

int  指两个字符串之间的词法关系

CompareTo

public int CompareTo(string strB)

Equals 方法

用于比较两个字符串是否相同

public bool Equals(string value);

public static bool Equals(string a ,string b);

格式化字符串

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

format:用来自定字符串所要格式化的形式

obj:要被格式化的对象

返回值:format的一个副本

string strA = "哈哈哈"; string strB = "呵呵呵"; string newString = string.Format("{0},{1}!!!",strA,strB); Console.WriteLine("新的字符串是:{0}",newString); DateTime datatimeNow = DateTime.Now; string newDataTime = string.Format("{0:D}",datatimeNow); Console.WriteLine("现在的时间是:{0}",newDataTime); Console.ReadLine();

截取字符串

截取字符串要用到string类的Substring方法。

public string Substring(int startIndex,int length); 

startIndex:字符串的起始位置的索引

length:字符串中的字符数

返回值:string

例子

class Program { static void Main(string[] args) { string strAllPath = "D:\\SubString测试程序\\SubString测试程序.exe"; string strPath = strAllPath.Substring(0,strAllPath.LastIndexOf("\\")+1); string strName = strAllPath.Substring(strAllPath.LastIndexOf("\\")+1); Console.WriteLine("文件路径是:{0}",strPath); Console.WriteLine("文件名是:{0}",strName); Console.ReadLine(); } }

 

 分隔字符串

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

separator 数组包含分隔的字符

返回值:按照条件分割后的字符数组

例子:

class Program { static void Main(string[] args) { Console.WriteLine("请输入一段话:"); string strOld = Console.ReadLine(); string[] strNews = strOld.Split(''); foreach(string m in strNews) { Console.WriteLine(m.ToString()+"。\n"); } Console.ReadLine(); } }

插入字符串

public string Insert(int startIndex,string Value);

startIndex 指定插入位置的索引,从零开始

Value      要插入的值

class Program { static void Main(string[] args) { string strOld = "你好!"; string strNew = strOld.Insert(0,"靓女,"); Console.WriteLine(strNew); Console.ReadLine(); } }

填充字符串

public string PadLeft(int TotalWidth,char paddingChar);

TatalWidth 结果中的字符总数

paddingChar 填充字符

返回值 一个新的字符串。

public string PadRight(int TotalWidth,char paddingChar);

删除字符串

 public string Removing(int startIndex);

public string Removing(int startIndex,int count);

复制字符串

public static string Copy(string str);

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

public void  string CopyTo(int sourceIndex,char[] destination,int dextinationIndex,int Count );

sourceIndex 需要复制的字符的起始位置

destination  目标数组

destinationIndex 指定目标数组中开始的位置

Count   复制的字符长度

字符串替换

public string Replace(string OValue,string NValue);

public string Replace(Char OChar,Char NChar);

 

转载于:https://www.cnblogs.com/lovezhangyu/p/3369959.html

最新回复(0)