IO操作专题
String
常用 API
网址推荐: C# 字符串(String) | 菜鸟教程
课程内容:
public class StringTest : MonoBehaviour
{
void Start()
{
Test1();
}
void Test1()
{
string str = "VIPSKILL";
// 1.Length
Debug.Log("str.Length:" + str.Length);
// 2.字符串比较的几种方式
string str2 = "vipskill";
// Compare 返回值 == 0;代表两个字符串相等;非 0,则代表不相等 (注意:在C#中,字符串对大小写是敏感的)
// 方法一
Debug.Log("string.Compare:" + string.Compare(str, str2));
// 方法二
if (str == str2)
Debug.Log("str == str2");
else
Debug.Log("str != str2");
// 方法三 - 1
Debug.Log("string.Equals:" + string.Equals(str, str2));
// 方法三 - 2
Debug.Log("str.Equals:" + str.Equals(str2));
Debug.Log("string.Compare(ignorecase):" + string.Compare(str, str2,true)); // 忽略大小写
// 3.字符串拼接 - VIPSKILLvipskill
Debug.Log("string.Concat(str.str2):" + string.Concat(str,str2));
Debug.Log("str + str2:" + (str + str2));
Debug.Log(string.Format("name:{0}, age:{1}", "张三", 14)); // 格式化
// 4.判断一个字符串中是否存在 目标字符串
Debug.Log("str.Contains(L):" + str.Contains("L"));
Debug.Log("str.Contains(l):" + str.Contains("l"));
// Debug.Log("str.Contains(l)" + str.Contains('L')); 错误的,只能判断字符串,不能判断字符
Debug.Log("str.Contains(l):" + str.IndexOf("l"));
Debug.Log("str.Contains(L):" + str.IndexOf("L"));
Debug.Log("str.Contains(L):" + str.IndexOf('L'));
Debug.Log("str.LastIndexOf(L):" + str.LastIndexOf('L'));
Debug.Log("str.StartsWith(L):" + str.StartsWith("L"));
Debug.Log("str.StartsWith(V):" + str.StartsWith("V")); // 区分大小写
Debug.Log("str.EndsWith(L):" + str.EndsWith("L"));
Debug.Log("str.EndsWith(V):" + str.EndsWith("V"));
// 5.insert 插入
Debug.Log("str.Insert(0,xxx):" + str.Insert(0, "xxx") + ", source str: " + str);
Debug.Log("str.Insert(0,xxx):" + str.Insert(2, "xxx") + ", source str: " + str);
// 6.static IsNullOrEmpty
//str = "";
Debug.Log("string.IsNullOrEmpty(str):" + string.IsNullOrEmpty(str));
// 7.Remove 移除
Debug.Log("str.Remove(0,2):" + str.Remove(0, 2) + ", source str: " + str);
// 8.Replace 替换
Debug.Log("str.Replace(V, v):" + str.Replace("V", "v") + ", source str: " + str);
// 9.Split 切割
string[] array = str.Split(new char[]{'P'});
for (int i = 0; i < array.Length; i++)
Debug.Log("str.Split(new char[] { 'P' }), i:" + i + ",content:" + array[i]);
// 10.ToLower 将字符串的字母改成小写;ToUpper 将字符串的字母改成大写
Debug.Log("str.ToLower():" + str.ToLower() + ", str2.ToUpper():" + str2.ToUpper());
// 11.Trim 去掉空格
string str3 = " abc";
//Debug.Log("str3:"+str3);
Debug.Log("str3.Trim():" + str3.Trim());
//str3.TrimStart
//str3.TrimEnd
}
}
StringBuilder
常用API
博客推荐: C#中StringBuilder类的使用总结_思乡游的博客-CSDN博客
课程内容:
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
public class StringBuilderTest : MonoBehaviour
{
void Start()
{
Test1();
}
void Test1()
{
StringBuilder sb = new StringBuilder("Hello World!"/*, 25*/);
sb.Capacity = 25;
// 1.打印 sb 长度和容量
Debug.Log("sb length: " + sb.Length + ", Capacity: " + sb.Capacity);
// 2.常用的 API
// 2.1 Append 末尾追加
sb = sb.Append(" What a beautiful day.");
Debug.Log("sb.Append: " + sb.ToString()); // Hello World!What a beautiful day.
// 2.2 AppendFormat 格式化追加
sb = sb.AppendFormat("{0} ", "VIPSKILL");
Debug.Log("sb.AppendFormat: " + sb.ToString()); // Hello World!What a beautiful day.VIPSKILL
// 2.3 Insert 指定位置插入
sb = sb.Insert(6, "Beautiful "); //
Debug.Log("sb.Insert: " + sb.ToString()); // Hello Beautiful World!What a beautiful day.VIPSKILL
// 2.4 Remove 移除
sb.Remove(5, 7);
Debug.Log("sb.Remove: " + sb.ToString()); // Hello ful World!What a beautiful day.VIPSKILL
// 2.5 Replace 替换
sb = sb.Replace('!', '?');
Debug.Log("sb.Replace: " + sb.ToString()); // Hello ful World?What a beautiful day.VIPSKILL
// 链式表达的概念
StringBuilder sb2 = new StringBuilder("Hello World!");
sb2 = sb2.Append("+").Append("-").Append("*");
Debug.Log("sb 链式表达写法: " + sb2.ToString()); // Hello World!+-*
}
}
String
和 StringBuilder
的区别
图解:
FileStream
文件读写
推荐网址: C#文件读写
图解:
演示:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
public class FileStreamTest : MonoBehaviour
{
void Start()
{
CreateFile();
ReadFile();
}
void CreateFile()
{
// 定义文件路径
string path = @"D:\some\c#\study\P1 Study\Assets\Resources\student.txt";
// 创建 File Stream 类的实例
FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
// 定义学号
string msg = "1710026";
// 将 字符串 转换为 字节数组
byte[] bytes = Encoding.UTF8.GetBytes(msg);
// 向文件中写入字节数组
fileStream.Write(bytes, 0, bytes.Length);
// 刷新缓冲区
fileStream.Flush();
// 关闭流
fileStream.Close();
}
void ReadFile()
{
// 定义文件路径
string path = @"D:\some\c#\study\P1 Study\Assets\Resources\student.txt";
// 判断是否含有指定文件
if (File.Exists(path))
{
FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
// 定义存放文件信息的字节数组
byte[] bytes = new byte[fileStream.Length];
// 读取文件信息
fileStream.Read(bytes, 0, bytes.Length);
// 将得到的字节型数组重写编码为 字符型数组
string c = Encoding.UTF8.GetString(bytes);
Debug.Log("学生的学号为:");
// 输出学生的学号
Debug.Log(c);
// 关闭流
fileStream.Close();
}
else
{
Debug.Log("您查看的文件不存在!");
}
}
}
StreamReader / Writer
文件读写
图解:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class StreamTest : MonoBehaviour
{
void Start()
{
//CreateFile();
ReadFile();
}
void CreateFile()
{
// 定义文件路径
string path = @"D:\some\c#\study\P1 Study\Assets\Resources\student2.txt";
// 创建 StreamWriter 类的实例
StreamWriter streamWriter = new StreamWriter(path);
// 向文件中写入姓名
streamWriter.WriteLine("小张");
// 向文件中写入手机号
streamWriter.WriteLine("13112345678");
// 刷新缓存
streamWriter.Flush();
// 关闭流
streamWriter.Close();
}
void ReadFile()
{
// 定义文件路径
string path = @"D:\some\c#\study\P1 Study\Assets\Resources\student2.txt";
// 创建 StreamReader 类的实例
StreamReader streamReader = new StreamReader(path);
// 判断文件中是否有字符
while(streamReader.Peek() != -1)
{
// 读取文件中的一行字符
string str = streamReader.ReadLine();
Debug.Log(str);
}
streamReader.Close();
}
}
Directory / DirectoryInfo
操作文件夹
图解:
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class DirectoryTest : MonoBehaviour
{
void Start()
{
TestDirectory();
TestDirectoryInfo();
}
void TestDirectory()
{
string strDir = @"D:\some\c#\study\P1 Study\Assets\Resources\DirTest";
// 创建文件夹
//Directory.CreateDirectory(strDir);
// 判断某路径的文件夹是否存在
//Directory.Exists(strDir);
// 删除指定的文件夹
//Directory.Delete(strDir);
//if (Directory.Exists(strDir))
//{
// Directory.Delete(strDir);
//}
//else
//{
// Directory.CreateDirectory(strDir);
//}
//Directory.Move(strDir, @"D:\some\c#\study\P1 Study\Assets\DirTest");
// 注意:若待删除的文件夹下面存在子文件夹。则需要传递 true,这样的话,将递归删除该文件夹下的子文件夹,同时删除自己
// 若待删除的文件夹下面不存在子文件夹,则默认进行直接删除即可
//Directory.Delete(@"D:\some\c#\study\P1 Study\Assets\DirTest",true);
//Directory.Delete(@"D:\some\c#\study\P1 Study\Assets\DirTest");
}
void TestDirectoryInfo()
{
string strDir = @"D:\some\c#\study\P1 Study\Assets\Resources\DirTest";
DirectoryInfo directoryInfo = new DirectoryInfo(strDir);
directoryInfo.Create();
// 生成子文件夹
directoryInfo.CreateSubdirectory("code-1");
directoryInfo.CreateSubdirectory("code-2");
//directoryInfo.Delete();
//directoryInfo.Delete(true);
IEnumerable<DirectoryInfo> dir = directoryInfo.EnumerateDirectories();
foreach(var v in dir)
{
Debug.Log(v.Name);
}
directoryInfo.MoveTo(@"D:\some\c#\study\P1 Study\Assets\DirTest");
}
}
C# Study