C#扫盲之:String字符串的常用方法和冷知识

时间:2022-12-13 16:09:59

前言

字符串对于任何编程语言都是必须操作和了解的,因为在实际编程中,任何项目和工程都必须要处理字符串数据,文件路径、提示消息,文本的处理等等,而在使用过程中很多人都是没有系统的了解,大量使用string,其实string的大量使用会造成很大的性能消耗,这跟.NET平台对字符串的处理有很大的关系,而.NET对字符串的支持还是很丰富的。不管是面试亦或者是实际编程使用,总结一下知识很有必要。

1.使用字符串数据

String提供了大量很多工具类,包括返回字符串长度、查找当前字符串中的字符串、转换大小写等方法,下图是String部分重要定成员

属性/方法 描述
Length  获取当前 System.String 对象中的字符数
Compare() 比较两个指定的 System.String 对象,返回一个整数标识二者之间的大小
Concat() 连接字符串
Contains() 判断当前字符串是否包含指定的一组字符串
Equals() 判断字符串是否与指定的字符串相等
Format() 格式化字符串
Insert() 插入字符串
PadLeft()
PadRight()
向左填充/向右填充
Remove() 从指定位置移除字符串
Replace() 替换指定支付为新对象
Split() 返回当前字符串由指定分割符分割的数组
StartsWith() 判断当前字符串是否由指定字符开始
Substring() 提取当前字符串的子字符串
ToCharArray() 转换成字符数组

ToLower()
ToUpper()

将当前字符串全部转换成小写/大写
Trim() 移除当前字符串头部和尾部所出现的自定字符,默认空格

以上就是几个比较重要的函数,有些是string类所有,有些是string对象所有

2.基本字符串操作

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Program
{
static void Main(string[] args)
{
string tmpStr = "you just come on,girl";
Printf(tmpStr.Length); //获取字符串长度
Printf(string.Compare("", ""));//比较 string str1 = "hehe";
string str2 = "haha";
Printf(str1 + str2);
Printf(string.Concat(str1, str2));//字符串拼接 Printf(tmpStr.Contains("just")); //包含关系
Printf("".Equals("123_")); //相等性
Printf(string.Format("{0} is great?but the {2} doesn't agree with{1}.", "xiaoMing", "xiaoHua", "her"));//格式化 tmpStr.Insert(, "insert"); //从字符3位置插入字符串"insert"
Printf(tmpStr); string intStr = "";
Printf(intStr.PadLeft(, '')); //设置字符串总长度未2,若原字符串长度不足则自动填'0'
Printf(intStr.PadLeft(, '')); //若字符串原长度超过2,则返回原字符串 string removeStr = "ha123hahhahhah12313";
removeStr.Remove(, ); //移除(从第3个字符串开始移除5个字符)
Printf(removeStr); string replaceStr = "xioaming";
replaceStr.Replace('o', 'M'); //替换(将源字符串中o替换成M) string splitStr = "xiaoming,xiaohua,xiaocao_xiaomao";
string[] splitArry = splitStr.Split(',', '_');//指定分割字符,分割源字符串返回字符串数组
foreach (string tmpstring in splitArry)
{
Console.Write(tmpstring + "");
}
string startStr = "heihieie";
bool isStartCondition = startStr.StartsWith("he", StringComparison.OrdinalIgnoreCase);//判断是否以字符串是否以“he”开始,并设置条件是按排序规则忽略大小写
bool isStart = startStr.StartsWith("he");
Printf(isStartCondition);
Printf(isStart); string subStr = "12341234xiangshouge";
Printf(subStr.Substring(, )); //从第11个字符开始获取2个字符, char[] charArry = subStr.ToCharArray();//转换成字符串
Printf(charArry.ToString()); string lowerStr = "xiaoming";
Printf(lowerStr.ToUpper()); //转换成大写
string uperStr="XIAOMINGHEHEDA";
Printf(uperStr.ToLower()); //转换成小写 string trimStr = " _123_heheda, ";
Printf(trimStr.Trim()); //去除字符串首尾空格,默认
Printf(trimStr.Trim(',')); Printf(trimStr.TrimStart(' ','_')); //去除字符串首部的空格和下划线
Printf(trimStr.TrimEnd(' ',',')); //去除字符串尾部,号 Console.ReadKey();
} /// <summary>
/// 打印函数
/// </summary>
static void Printf(object str)
{
Console.WriteLine(str.ToString());
}
static void Printf(int str)
{
Console.WriteLine(str.ToString());
}
}
}

输出:

C#扫盲之:String字符串的常用方法和冷知识

3.转义字符

C#字符串字面量中可以包含各种转义字符,用来限制字符数据在输出流中的格式,转义字符为反斜杠\,常见转义字符

字符 作用
\'  在字符串中标识单引号
\"  在字符串中标识双引号
\\  在字符串中标识\(常用于定义网络和文件路径)
\a  触发一个系统警报(蜂鸣)
\n  换行(windows平台上有效)
\r  回车
\t  水平制表符
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Program
{
static void Main(string[] args)
{
Printf("hahhehha \a");
Printf("C:\\heheda\bing");
Printf("1234\n\n\n");//添加三个空行
Printf("1231\t1231"); Console.ReadKey();
} /// <summary>
/// 打印函数
/// </summary>
static void Printf(object str)
{
Console.WriteLine(str.ToString());
}
static void Printf(int str)
{
Console.WriteLine(str.ToString());
}
}
}

输出:

C#扫盲之:String字符串的常用方法和冷知识

4.字符串的恒定性及性能影响

在.NET中,一旦将初始值赋给字符串对象,字符数据就不会改变了,不仔细了解还很疑惑,其实所有对string类型的方法操作都是返回一个新的字符串对象,所以这也就是为什么不能大量使用string类型的原因,特别是在考虑性能的程序和项目中,更应该注意减少使用string类型。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace StringTest
{
class Program
{
static void Main(string[] args)
{
//设置初始字符串值
string s1 = "this is my stringtest";
Console.WriteLine("s1 is={0}", s1); //大写s1
string UperStr = s1.ToUpper();
Console.WriteLine("UperStr is={0}", UperStr); Console.WriteLine("s1 is={0}", s1);//s1是大写还是小写呢?此处s1还是原来的值
Console.WriteLine("s1内存地址是否与s1.touper之后的内存地址相等:{0}", string.ReferenceEquals(s1, s1.ToUpper())); Console.ReadKey();
}
}
}

输出:

C#扫盲之:String字符串的常用方法和冷知识

可以看到两者的内存引用控件并不相同,产生了新字符串。

 字符串恒定性对性能影响分析:

class Program
{
static void Main(string[] args)
{
//设置初始字符串值
string s1 = "this is my stringtest";
s1 = "my seconde string test"; Console.ReadKey();
}
}

使用ILDasm.exe查看编译后的CIL代码如下:

C#扫盲之:String字符串的常用方法和冷知识

通过产看CIL代码可以看到程序多次使用ldstr操作码,它表示的是加载字符串,也就是在托管堆上申请空间并分配值,s1再被重新赋值时,旧的临时字符串"this is my stringtest“就会失效,s1指向了新的引用"my seconde string test",而1处支付串最终会被GC管理器回收。这种字符串在也就是说在程序中若存在多个不同的字符串,则会进行多次加载,大量的使用string类型将会变得低效,并导致代码膨胀,特别是进行字符串拼接的时候,这是因为编译器在编译写元数据时候回多次加载不同的字符串常量。

所以在大量处理文本数据的时候,建议使用StringBulder操作字符串,因为StringBulder对象直接在内存对象上对字符串对象进行修改,这样不会产生新的副本,使程序变得高效,StringBulder的详细讨论,本文暂时搁置。

由于本人才学识浅,描述难免纰漏,如有错误,欢迎指出。么么!