namespace 字符串方法练习
{
class Program
{
static void Main(string[] args)
{
string MyStr = " Hello World! ";
//长度
int leng = MyStr.Length;
Console.WriteLine(leng);
//SubStrings 截取字符串
string s = MyStr.Substring(0, MyStr.Length - 5);
Console.WriteLine(s);
char[] ch = MyStr.ToCharArray();
Console.WriteLine(ch[2]);
//转换为大写、小写
Console.WriteLine(MyStr.ToUpper());
Console.WriteLine(MyStr.ToLower());
//替换
Console.WriteLine(MyStr.Replace("H","W"));
//判断是否空值
bool bl = string.IsNullOrEmpty(MyStr);
Console.WriteLine(bl);
//开头
bool bol = MyStr.StartsWith(" He");
Console.WriteLine(bol);
//第一个出现的索引
int n1 = MyStr.IndexOf("o");
Console.WriteLine(n1);
//插入
string str_new1= MyStr.Insert(2, "fff");
Console.WriteLine(str_new1);
//移除
string str_new2 = MyStr.Remove(3, 2);
Console.WriteLine(str_new2);
//比较
string c1 = "abcdefg";
string c2 = "abcdefg";
int c12 = string.Compare(c1, c2);
Console.WriteLine(c12);
Console.ReadKey();
}
}
}
C# 字符串相关方法的介绍
最新推荐文章于 2024-12-05 09:39:38 发布