目录
一、简介
string
string,引用类型,string类型表示零或更多 Unicode 字符组成的序列,string 是 .NET Framework 中 String 的别名。C# 中定义相等运算符 ( == 和 != ) 是为了比较 string 对象(而不是引用)的值。
String
String是类,表示文本,即一系列 Unicode 字符。String 对象是不可改变的。每次使用System.String 类中的方法之一时,都要在内存中创建一个新的字符串对象,这就需要为该新对象分配新的空间。如:当我们实例化一个 String 的对象后,在内存中为此对象分配一个空间。如下: String str = "hello",当我们修改 str 的值的时候,如: str = "hello world",此时,系统会为 str 重新分配一个空间。这样原来的内存空间就被浪费掉了,只能等待垃圾回收器回收。在需要对字符串执行重复修改的情况下,与创建新的 String 对象相关的系统开销可能会非常昂贵。
二、常用的操作
1.字符串的查找
(1) IndexOf
Indexof 是从字符串的前面往后面查找的。
这里有两个重载:
Indexof(Char C): 找到第一个字符c的index,如果没找到返回-1
Indexof(string str): 找到str的index,如果没找到返回-1
案例:
string content = "唱,跳,rap,篮球!";
int index1 = content.IndexOf(',');
int index2 = content.IndexOf("rap");
int index3 = content.IndexOf("会");
Console.WriteLine("index:" + index1);
Console.WriteLine("index:" + index2);
Console.WriteLine("index:" + index3);
打印:
(2) LastIndexOf
返回当前字符或字符串的最后一个匹配项位置
案例:
string content = "唱,跳,rap,篮球!";
int index1 = content.LastIndexOf(',');
int index2 = content.LastIndexOf("rap");
int index3 = content.LastIndexOf("喜欢");
Console.WriteLine("index:" + index1);
Console.WriteLine("index:" + index2);
Console.WriteLine("index:" + index3);
打印:
这里需要从字符串的后面向前面数,就得出打印中的这些数据
(3) 查找字符的下标集合
上面的两种方式,只能找出一个字符串的一个下标,下面介绍如何查询单个字符的下标集合
/// <summary>
/// 查找字符串
/// </summary>
/// <param name="content">字符串</param>
/// <param name="substr">要查找的内容</param>
/// <param name="StartPos">查询开始的下标</param>
/// <returns></returns>
public int[] GetSubStrCountInStr(String content, String substr, int StartPos)
{
int foundPos = -1;
int count = 0;
List<int> foundItems = new List<int>();
do
{
foundPos = content.IndexOf(substr, StartPos);
if (foundPos > -1)
{
StartPos = foundPos + 1;
count++;
foundItems.Add(foundPos);
}
} while (foundPos > -1 && StartPos < content.Length);
return ((int[])foundItems.ToArray());
}
用法看方法的注释
2.字符串的替换
字符串替换主要有两个重载
public string Replace(Char first, Char second)
public string Replace(String firstString, String secondString)
案例:
string content = "唱,跳,rap,篮球!";
string newContent = content.Replace("rap", "打鸣");
Console.WriteLine(newContent);
打印:
3.字符串的分割
字符串的分割用法有多种,最常用的是下面这两种
案例1:以 char 类型符号分割
string content = "唱,跳,rap,篮球!";
string[] Arr = content.Split(',');
for (int i = 0; i < Arr.Length; i++)
{
Console.WriteLine("分割:" + Arr[i]);
}
打印:
案例2:以字符串分割
string content = "唱,跳,rap,篮球!";
string[] Arr = Regex.Split(content, "rap", RegexOptions.IgnoreCase);
for (int i = 0; i < Arr.Length; i++)
{
Console.WriteLine("分割:" + Arr[i]);
}
打印:
4.字符串的合并
(1) "+" 运算符
连接字符串,这种使用方式,效率特别低
string str1 = "节日";
string str2 = str1 + "快乐";
Console.WriteLine(str2);
打印:
(2) Concat 方法
C# Concat() 方法用于连接多个字符串对象。它返回连接的字符串。 Concat() 的重载方法有很多。
public static string Concat(String, String)
public static string Concat(IEnumerable<String>)
public static string Concat(Object)
public static string Concat(Object, Object)
public static string Concat(Object, Object, Object)
public static string Concat(Object, Object, Object, Object)
public static string Concat(params Object[])
public static string Concat(String, String, String)
public static string Concat(String, String, String,?String)
public static string Concat(params String[])
[ComVisibleAttribute(false)]
public static string Concat<T>(IEnumerable<T>)
案例:
string str1 = "节日";
string str2 = "快乐";
string str3 = string.Concat(str1, str2);
Console.WriteLine(str3);
打印:
(3) Join 方法
C# 中的 Join() 方法用于连接字符串数组的所有元素,并在每个元素之间使用指定的分隔符。
构造函数
//在字符串数组中间加入separator字符串,返回新的字符串
public static String Join(String separator, params String[] value);
//在object数组中间加入separator字符串,返回新的字符串
public static String Join(String separator, params object[] values);
案例:
string[] nameArr = { "张三", "李四", "王五", "刘六" };
string result = string.Join("-", nameArr);
Console.WriteLine(result);
打印:
Join 还有一些花哨的操作,这里我就不演示了,平时用的比较少
5.字符串的插入
向字符串中插入新的字符,组成一个新的字符串
构造函数
public String Insert(int startIndex, String value);
startIndex 是插入的位置
value 为插入的内容
案例:
string content = "喜欢唱,跳,rap,篮球";
string newContent = content.Insert(content.Length, ",music");
Console.WriteLine(newContent);
打印:
6.字符串的删除
字符串的删除,常用的有下面两个
//将下标为startIndex(包括startIndex)到字符串后面删除
public String Remove(int startIndex);
//将下标为startIndex(包括startIndex)到后面count个字符删除
public String Remove(int startIndex, int count);
案例1:
string content = "喜欢唱,跳,rap,篮球";
string newContent = content.Remove(5);
Console.WriteLine(newContent);
打印:
从第五个字符开始(包含第五个字符在内)到字符串的最后全部删除
案例2:
string content = "喜欢唱,跳,rap,篮球";
string newContent = content.Remove(4, 2);
Console.WriteLine(newContent);
打印:
7.字符串的截取
常用的有下面两种
//截取从下标startIndex(包括startIndex)到最后的字符串
public String Substring(int startIndex);
//从下标startIndex(包括startIndex)开始截取length长度的字符串
public String Substring(int startIndex, int length);
案例1:
string content = "喜欢唱,跳,rap,篮球";
string newContent = content.Substring(6);
Console.WriteLine(newContent);
打印:
案例2:
string content = "喜欢唱,跳,rap,篮球";
string newContent = content.Substring(6, 3);
Console.WriteLine(newContent);
打印:
8.字符串去空格
构造函数
public String Trim(); //去掉字符串首尾默认字符(空格)
public String Trim(params char[] trimChars); //去掉字符串首尾的自定义字符
public String TrimStart(params char[] trimChars); //去掉字符串首部默认字符(空格)
public String TrimEnd(params char[] trimChars); //去掉字符串尾部默认字符(空格)
案例
String content = " 吃 瓜 群 众";
string newContent = content.Trim();
Console.WriteLine(newContent);
打印:
9.字符串的转换
(1) ToUpper
将字符串转化成大写形式
案例:
string content = "abcdef";
string newContent = content.ToUpper();
Console.WriteLine(newContent);
打印:
(2) ToLower
将字符串转化成小写形式
案例:
string content = "ABCEDEG";
string newContent = content.ToLower();
Console.WriteLine(newContent);
打印:
10.字符串的判断
(1) StartsWith
判断字符串是否以字符串value开头
案例:
string content = "喜欢唱,跳,rap,篮球";
bool result = content.StartsWith("喜欢");
Console.WriteLine("执行结果:" + result);
打印:
(2) EndsWith
判断字符串是否以字符串value结尾
案例:
string content = "喜欢唱,跳,rap,篮球";
bool result = content.EndsWith("篮球");
Console.WriteLine("执行结果:" + result);
打印:
(3) Contains
判断字符串是否包含字符串value
案例:
string content = "喜欢唱,跳,rap,篮球";
bool result = content.Contains("rap");
Console.WriteLine("执行结果:" + result);
打印:
(4) IsNullOrEmpty
判断字符串是否为null或空
案例:
string content = "";
bool result = string.IsNullOrEmpty(content);
Console.WriteLine("是否为空:" + result);
打印:
11.字符串的比较
(1) Compare
比较两个字符串, 返回一个 int 来表明比较关系。
比较字符串A和字符串B,如果返回-1,说明A小于B;返回0,说明A等于B;返回1,说明A大于B。
案例:
string str1 = "C# study";
string str2 = "C#";
int b1 = String.Compare(str1, str2);
string welcome = "";
switch (b1)
{
case 0:
welcome = "两个字符串相等!";
break;
case 1:
welcome = "第一个字符串大";
break;
case -1:
welcome = "第二个字符串大";
break;
}
Console.WriteLine(welcome);
打印:
(2) CompareTo
用法和上节的 Compare 差不多
案例:
string str1 = "C# study";
string str2 = "C#";
int b1 = str1.CompareTo(str2);
string welcome = "";
switch (b1)
{
case 0:
welcome = "两个字符串相等!";
break;
case 1:
welcome = "第一个字符串大";
break;
case -1:
welcome = "第二个字符串大";
break;
}
Console.WriteLine(welcome);
打印:
(3) Equals
比较两个字符串是否相同
//判断字符串是否相等。返回【True】,说明相等;返回【False】说明不相等
public bool Equals(String value);
//选定的枚举比较方式,例如:忽略大小写
public bool Equals(String value, StringComparison comparisonType);
public static bool Equals(String a, String b);
案例
string str1 = "C# study";
string str2 = "C#";
bool res1 = str1.Equals(str2);
Console.WriteLine("res1是否相同:"+ res1);
bool res2 = Equals(str1, str2);
Console.WriteLine("res2是否相同:" + res2);
打印:
12.字符串的复制
(1) Copy
复制字符串,返回新字符串
案例
string str1 = "100";
string str2 = string.Copy(str1);
(2) CopyTo
CopyTo 也是比较好用的一个方法,至于为什么用这个方法,我先写一个演示。
案例1。
int[] arr1 = new int[4] { 1, 3, 7, 2 };
Console.WriteLine("arr1:" + string.Join(" | ", arr1));
int[] arr2 = arr1;
arr2[2] = 9;
Console.WriteLine("arr1:" + string.Join(" | ", arr1));
打印:
从上面打印中可以看到,在 arr1 赋值给 arr2 后,我们改变 arr2 的值,arr1 内数据也会随着一起改变,那么如果我想把 arr1 中的数据赋值到 arr2 中,也不影响 arr1 怎么办, CopyTo 关键字刚好可以解决这个问题。
案例:
int[] arr1 = new int[4] { 1, 3, 7, 2 };
Console.WriteLine("arr1:" + string.Join(" | ", arr1));
int[] arr2 = new int[arr1.Length];
arr1.CopyTo(arr2, 0);
arr2[2] = 9;
Console.WriteLine("arr1:" + string.Join(" | ", arr1));
Console.WriteLine("arr2:" + string.Join(" | ", arr2));
打印:
这次,改变了 arr2 的值后,arr1 内部数据也没有改变,那我们想要的效果就达到了,至于原因,有兴趣的可以看下 值类型和引用类型 的区别就能明白是为什么了。
13.字符串的格式化
在字符串格式化,特殊字母的含义
字母 | 含义 |
---|---|
C | 专用场合的货币值 |
D | 一般整数(只适用整数类型) |
E | 科学计数法 |
F | 小数点后的位数固定 |
G | 一般整数 |
N | 通用场合数字格式 |
P | 百分数 |
X | 十六进制格式(只适用整数类型) |
案例:
Console.WriteLine(string.Format("默认格式:{0}", 2022));
Console.WriteLine(string.Format("C格式:{0:C}", 2022));
Console.WriteLine(string.Format("D格式:{0:D}", 2022));
Console.WriteLine(string.Format("E格式:{0:E}", 2022));
Console.WriteLine(string.Format("F格式:{0:F}", 2022));
Console.WriteLine(string.Format("G格式:{0:G}", 2022));
Console.WriteLine(string.Format("N格式:{0:N}", 2022));
Console.WriteLine(string.Format("P格式:{0:P}", 2022));
Console.WriteLine(string.Format("X格式:{0:X}", 2022));
//其他输出方式
Console.WriteLine(string.Format("F格式:{0:D5}", 02022));//按5位输出整数
Console.WriteLine(string.Format("F格式:{0:F3}", 2022));//按固定小数点后三位输出小数
输出:
结束
如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢
end