C# 字符与字符串操作
在C#中,字符和字符串是两个重要的数据类型,有许多内置的方法可以处理字符和字符串。这些方法是非常有用的,可以帮助开发人员更方便、更高效地处理文本数据。
格式化字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
// 取出字符串中指定的字符
char Str2 = Str1[1];
Console.WriteLine("字符: {0} 转大写: {1} 转小写: {2}", Str2, Char.ToUpper(Str2), Char.ToLower(Str2));
Console.WriteLine("是否为数字: {0} 是否为大写: {1} 是否为小写: {2}",
Char.IsNumber(Str2), Char.IsUpper(Str2), Char.IsLower(Str2));
// 将字符串转化为字符数组
char[] chs = Str1.ToCharArray();
for (int x = 0; x < chs.Length - 1; x++)
Console.Write("{0} ", chs[x]);
Console.WriteLine();
// 将字符数组转化为字符串
string Str3 = new string(chs);
Console.WriteLine(Str3);
// 格式化输出字符串
string Str4 = "hello";
string Str5 = "lyshark";
string new_str = String.Format("{0},{1}", Str4, Str5);
Console.WriteLine("格式化后的字符串: {0}", new_str);
Console.ReadKey();
}
}
}
比较字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string Str1 = "hello lyshark";
string Str2 = "hello world";
string Str3 = "hello lyshark";
// Compare 比较字符串,相等返回0不相等返回-1
Console.WriteLine("Str1 比较 Str2 " + String.Compare(Str1, Str2));
Console.WriteLine("Str1 比较 Str3 " + String.Compare(Str1, Str3));
// Compare To 比较字符串
Console.WriteLine("Str1 比较 Str2 " + Str1.CompareTo(Str2));
Console.WriteLine("Str1 比较 Str3 " + Str1.CompareTo(Str3));
// Equals 比较字符串
Console.WriteLine("Str1 比较 Str2 " + Str1.Equals(Str2));
Console.WriteLine("Str1 比较 Str3 " + String.Equals(Str1,Str3));
Console.ReadKey();
}
}
}
截取/分割字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 从第一个位置开始截取3个字符
string Str1 = "hello lyshark";
string Str2 = "";
Str2 = Str1.Substring(1, 3);
Console.WriteLine("截取数据: {0}", Str2);
// 分割字符串变量
string Str3 = "用^一生#下载,百度网盘,资源";
char[] separator = { '^', '#', ',' }; // 定义分割字符
String[] split_string = new String[100];
split_string = Str3.Split(separator);
for (int x = 0; x < split_string.Length;x++ )
{
Console.WriteLine("切割计数: {0} 切割字符串: {1}", x, split_string[x]);
}
// 针对时间的切割方法
string str = "2019-12-12";
char[] chs = { '-' };
string[] date = str.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("{0}年 {1}月 {2}日", date[0], date[1], date[2]);
Console.ReadKey();
}
}
}
插入/删除字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 插入字符串的演示
string Str1 = "下载";
Str1 = Str1.Insert(0, "用一生时间");
Console.WriteLine(Str1);
string Str2;
Str2 = Str1.Insert(Str1.Length, "百度网盘里面的资源");
Console.WriteLine(Str2);
// 填充字符串的演示
string Str3;
Str3 = Str1.PadLeft(Str1.Length + 3, '*'); // 在左侧填充
Console.WriteLine("左侧填充: " + Str3);
Str3 = Str1.PadRight(Str1.Length + 3, '*'); // 在右侧填充
Console.WriteLine("右侧填充: " + Str3);
// 去空格的实现
string str = " hahahah ";
str = str.Trim();
str = str.TrimStart();
str = str.TrimEnd();
// 删除字符串的演示
Console.WriteLine("从索引3处向后删除: " + Str3.Remove(3));
Console.WriteLine("删除指定个数的字符: " + Str3.Remove(1, 3));
Console.ReadKey();
}
}
}
拷贝替换字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 普通的拷贝字符串
string Str1 = "hello lyshark";
string Str2;
Str2 = string.Copy(Str1);
Console.WriteLine("普通拷贝: " + Str2);
// 替换字符串
string Str3 = "one world,one dream";
string Str4 = Str3.Replace(',','*');
Console.WriteLine("将,替换为** => " + Str4);
string Str5 = Str3.Replace("one", "One");
Console.WriteLine("将one替换为One =>" + Str5);
Console.ReadKey();
}
}
}
寻找开头结尾字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "今天天气好晴朗,处处好风光";
// 寻找字符串开头结尾
if (str.StartsWith("今") && str.EndsWith("光"))
Console.WriteLine("ok");
// 从指定字符开始搜索,并返回位置
int index = str.IndexOf("天气", 0);
Console.WriteLine(index);
// 从结束位置开始搜索
string path = @"c:\a\b\c苍\d\e苍\f\g\\fd\fd\fdf\d\vfd\苍老师.wav";
int path_index = path.LastIndexOf("\\");
Console.WriteLine(path_index);
Console.ReadKey();
}
}
}
串联字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Join 将指定字符串使用|串联起来
string name_str = string.Join("|", "张三", "李四", "王五", "赵六", "田七");
Console.WriteLine(name_str);
// 将字符串切割后串联去掉竖线
String[] u_name = { "张三", "李四" ,"王五"};
string ptr = string.Join("|", u_name);
Console.WriteLine("合并后: " + ptr);
string[] strNew = ptr.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine("去掉| = " + strNew[1]);
for (int x = 0; x < strNew.Length;x++ )
Console.WriteLine("去掉竖线 [{0}] = {1}",x,strNew[x]);
Console.ReadKey();
}
}
}
字符串倒序输出:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "hello lyshark";
// 实现反转字符串 hello -> olleh
char[] chs = str.ToCharArray();
for (int x = 0; x < chs.Length / 2;x++ )
{
char tmp = chs[x];
chs[x] = chs[chs.Length - 1 - x];
chs[chs.Length - 1 - x] = tmp;
}
str = new string(chs);
Console.WriteLine("反转后的结果: {0}", str);
// 实现反转单词 hello lyshark -> lyshark hello
string str1 = "hello lyshark";
string[] strNew = str1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
for (int x = 0; x < strNew.Length / 2; x++)
{
string tmp = strNew[x];
strNew[x] = strNew[strNew.Length - 1 - x];
strNew[strNew.Length - 1 - x] = tmp;
}
str1 = string.Join(" ", strNew);
Console.WriteLine("反转后的结果: {0}", str1);
Console.ReadKey();
}
}
}
IndexOf搜索字符串:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// 通过indexOf 切割特定字符
string email = "admin@blib.cn";
int index = email.IndexOf("@");
string userName = email.Substring(0, index);
string userHost = email.Substring(index + 1);
Console.WriteLine("名字: {0} 主机: {1}",userName,userHost);
// 寻找指定字符出现位置
string str = "abcd wwabcd asdcdsac waascd ascsaaa";
int index1 = str.IndexOf('a');
int count = 1;
while(index1 != -1)
{
count++;
index1 = str.IndexOf('a', index1 + 1);
if (index1 == -1)
break;
Console.WriteLine("第{0}次出现a的位置是{1}", count, index1);
}
Console.ReadKey();
}
}
}
文章出处:https://www.cnblogs.com/LyShark/p/13156497.html
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?