字符串(String):
注意字符串是不可變的,所以這些函數都不會直接改變字符串的內容,而是把修改后的字符串的值通過函數返回值的形式返回。s.ToLower()與s=s.ToLower()不同:前一個得到s轉換后的一個副本,s本身沒變;后個將s轉換后的副本賦給s,s指向變了,但原字符串還存在。
ToLower():得到字符串的小寫形式
ToUpper():得到字符串的大寫形式
Trim()去掉字符串兩端的空白
Equals()比較方法 。 s1.Equals(s2,StringComparison.OrdinallgnoreCase),兩個字符串進行比較不區分大小寫的比較
string[] Split(char[] separator,StringSplitOptions.options):參數指定是否返回空數組元素。options為None時,返回有空元素;為RemoveEmptyEntries時,返回值沒有空元素。
string Replace(string oldValue,string newValue);字符串替換。將字符串中出現oldValue的地方替換為newValue.
string Substring(int startIndex);取從位置startIndex開始一直到最后的子字符串。
string Substring(int startIndex,int length);取從位置startIndex開始長度為length的子字符串,如果子字符串的長度不足length則報錯。
bool Contains(string value);判斷字符串中是否含有子串value
bool StartsWith(string value);判斷字符串是否以子串value開始
bool EndsWith(string value);判斷字符串是否以子串value結束
int IndexOf(string value);取子串value第一次出現的位置
int IndexOf(string value,int startIndex);從startIndex位開始查找,取子串value第一次出現的位置
string string.Format(string format,object arg0);格式化字符串
bool string.IsNullOrEmpty(string value);判斷字符串value是null還是System.String.Empty字符串
----------------------------
Path//對文件或目錄的路徑進行操作
string ChangeExtension(string path, string extension) 修改文件的后綴,“修改”支持字符串層面的,沒有真的給文件改名
string s = Path.ChangeExtension(@"C:\temp\F3.png", "jpg")
string Combine(string path1, string path2) 將兩個路徑合成一個路徑,比用+好,可以方便解決不加斜線的問題,自動處理路徑分隔符的問題
string s = Path.Combine(@"c:\temp","a.jpg")
string GetDirectoryName(string path) 得到文件的路徑名。Path.GetDirectoryName(@"c:\temp\a.jpg")
string GetExtension(string path) 得到文件的擴展名
string GetFileName(string path) 得到文件路徑的文件名部分
string GetFileNameWithoutExtension(string path) 得到去除擴展名的文件名
string GetFullPath(string path) 得到文件的全路徑。可以根據相對路徑獲得絕對路徑。
string GetTempFileName() 得到一個唯一的臨時文件名(*)
string GetTempPath() 得到臨時文件夾的路徑(*)
--------------------------------
Directory //操作目錄
•void Delete(string path, bool recursive) 刪除目錄, recursive表示是否遞歸刪除,如果recursive為false則只能刪除空目錄
•bool Exists(string path) 判斷目錄是否存在
•string[] GetDirectories(string path) 得到一個目錄下的子目錄
•string[] GetDirectories(string path, string searchPattern, SearchOption searchOption) 通配符查找目錄下的子目錄,可以搜索到隱藏文件。
•static string[] GetFiles(string path) 得到一個目錄下的文件
•string[] GetFiles(string path, string searchPattern, SearchOption searchOption) 通配符查找目錄下的文件
•DirectoryInfo GetParent(string path) 得到目錄的父目錄
•move() //移動、剪切。只能在同一個磁盤中。目錄沒有Copy方法。可以使用Move()方法實現重命名。
•create()
-----------------------
File //操作文件
•File.
Copy(“source”, “targetFileName”, true);
//
文件拷貝
,true
表示當文件存在時
“
覆蓋
”
,如果不加
true,
則文件存在報異常
•File.Move(“source”, “target”);
//
移動(剪切),
•File.Delete(“path”);
//
刪除
•File.Create(“path”);
//
創建文件
•bool Exists(string path)判斷文件path是否存在
--操作文本文件
•void AppendAllText(string path, string contents),將文本contents附加到文件path中
•string[] ReadAllLines(string path) 讀取文本文件到字符串數組中
•string ReadAllText(string path) 讀取文本文件到字符串中
•string ReadAllBytes(string path) 讀取文本文件到字符串中
•void WriteAllText(string path, string contents)將文本contents保存到文件path中,會覆蓋舊內容。
•WriteAllLines(string path,string[] contents),將字符串數組逐行保存到文件path中,會覆蓋舊內容。
--快速得到文件流
FileStream fs=File.Open(); //返回FileStream
FileStream fs=File.OpenRead();//返回只讀的FileStream
FileStream fs=File.OpenWrite();//返回只寫的FileStream