您的位置:首页 > 其它

String、Path、File、Directroy 常用方法总结

2013-03-21 12:25 288 查看
字符串(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: