C#字符串操作
C#字符串操作
一、字符串操作常用方法
- ToUpper():将字符转换成大写形式,仅对字母有效。返回值是转换后的字符串。
- ToLower():将字符转换成小写形式,仅对字母有效。返回值是转换后的字符串。
- Equals():比较两个字符串是否相同。传入字符串,相同返回真,不相同返回假。
- Split():分割字符串。传入字符数组,返回字符串数组。
- Substring():截取字符串。返回截取后的子串。
- IndexOf():查找某个字符串在字符串中第一次出现的位置。 返回所在的索引位置值。如果没有找到,返回-1。
- LastIndexOf():查找某个字符串在字符串中最后一次出现的位置。 返回所在的索引位置值。如果没有找到,返回-1。
- startsWith():判断是否以…字符串开始。如果是,返回真;如果不是,返回假。
- EndsWith():判断是否以…字符串结束。如果是,返回真;如果不是,返回假。
- Replace():将字符串中的某个子串全部替换成一个新的字符串。返回新的字符串。
- Contains():判断某个字符串中是否包含指定的字符串。如果包含返回真,否则返回假。
- Trim():去掉字符串中前后空格。返回处理后的字符串。
- TrimEnd() 作用:去掉字符串结束后的空格。返回处理后的字符串。
- TrimStart() 作用:去掉字符串开始前的空格。返回处理后的字符串。
- IsNullOrEmpty() 作用:判断一个字符串是否为 Null 或者空。 如果为 null 或者空,返回真;否则返回假。
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 = "HelloWorld";
// 1.ToUpper():字母全部大写
Console.WriteLine(s1.ToUpper()); //HELLOWORLD
// 2.ToLower():字母全部小写
Console.WriteLine(s1.ToLower()); //helloworld
// 3.Equals(String s):比较两字符串内容
string s2 = "jojo";
Console.WriteLine(s1.Equals(s2)); //false
// 4.Split():分割字符串
string s3 = "中国|河南|郑州";
char[] chs = { '|' };
string[] s4 = s3.Split(chs);
foreach (string s in s4)
{
Console.WriteLine(s); //依次打印三行:中国,河南,郑州
}
// 5.Substring():截取字符串
string s5 = s1.Substring(0, 5); //Hello
string s6 = s1.Substring(5, 5); //World
Console.WriteLine(s5);
Console.WriteLine(s6);
// 6.IndexOf():查找子串第一次出现位置
int index1 = s1.IndexOf("World");
int index2 = s1.IndexOf("jojo");
Console.WriteLine(index1); //5
Console.WriteLine(index2); //-1
// 7.LastIndexOf():查找子串最后一次出现位置
int index3 = s1.LastIndexOf("l"); //8
int index4 = s1.LastIndexOf("jojo"); //-1
Console.WriteLine(index3);
Console.WriteLine(index4);
// 8.startWith():是否已...字符串开始
Console.WriteLine(s1.StartsWith("Hello")); //True
// 9.EndsWith():是否已...字符串结束
Console.WriteLine(s1.EndsWith("World")); //True
// 10.Replace():将字符串中某个字串全都替换成新的字符串
string s7 = "HelloWorldHello";
string s8 = s7.Replace("Hello", "jojo");
Console.WriteLine(s8); //jojoWorldjojo
// 11.Contains():判断某个字符串中是否包含指定字串
Console.WriteLine(s1.Contains("llo")); //True
// 12.Trim():去掉字符串中前后的空格
string s9 = " Golden Wind~ ";
Console.WriteLine(s9.Trim()); //Golden Wind~(结束)
// 13.TrimEnd():去掉字符串后面的空格
Console.WriteLine(s9.TrimEnd()); // Golden Wind~(结束)
// 14.TrimStart():去掉字符串前面的空格
Console.WriteLine(s9.TrimStart()); //Golden Wind~ (结束)
// 15.isNullOrEmpty():判断字符串是否为null或者空
Console.WriteLine(string.IsNullOrEmpty(s1)); //False
string s10 = "";
string s11 = null;
Console.WriteLine(string.IsNullOrEmpty(s10)); //True
Console.WriteLine(string.IsNullOrEmpty(s11)); //True
Console.ReadKey();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
二、字符串的特点
1.字符串是引用类型
字符串的数据是存储在堆空间,在栈空间中存储了该数据的引用地址。
2.字符串是不可变的
当你给一个字符串变量重新赋值时,旧值并没有销毁,而是重新开辟一块空间来存储新值。
3.字符串可以看做字符数组
使用字符串变量[下标]可以取字符串中指定的字符。也可以使用 for 循环变量数组。字符串变量.Length;可以取得字符串字符的个数。
三、StringBuilder
1.StringBuilder 简介
1.字符串的缺点
当需要对一个字符串变量重复赋值时,在内存中会产生大量的垃圾数据信息。 当重复赋值的频率很高时,执行的效率就会降低。
2.StringBuilder 简介
String,字符串;Builder,构建器;连起来是“字符串构建器”。 StringBuilder 类型的“字符串变量”,一直操作同一块内存空间,不会产生垃圾数据,且执行效率远远高于 string 类型的字符串变量。
2.StringBuilder 使用方法
PS:StringBuilder 依赖 System.Text 命名空间。
- Append():追加数据
- ToString():转换为字符串
- sb.Clear():清空
3.StringBuilder效率
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TimeTest
{
class Program
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
/*
string s = "";
for (int i = 0; i <= 100000; i++)
{
s += i;
}
sw.Stop();
Console.WriteLine(sw.Elapsed); //十万条数据 用时:16.73s
*/
StringBuilder sb = new StringBuilder();
for (int i = 0; i <= 100000; i++)
{
sb.Append(i);
}
sw.Stop();
Console.WriteLine(sw.Elapsed); //十万条数据 用时:0.01s
Console.ReadKey();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
补充:
1.Stopwatch 类
Stopwatch,秒表计时器,用来记录程序运行的时间。
注意:Stopwatch 依赖 System.Diagnostics 命名空间。
2.创建 Stopwatch 类型对象
Stopwatch sw = new Stopwatch();
sw.Start(); //计时器开始。
sw.Stop(); //计时器结束。
1.Stopwatch 类
Stopwatch,秒表计时器,用来记录程序运行的时间。
注意:Stopwatch 依赖 System.Diagnostics 命名空间。
2.创建 Stopwatch 类型对象
Stopwatch sw = new Stopwatch();
sw.Start(); //计时器开始。
sw.Stop(); //计时器结束。
sw.Elapsed; //开始到结束之间的时长。
posted on 2022-10-20 15:21 8888888888888 阅读(337) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)