📄 class1.cs
字号:
using System;
namespace Example_String
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class MainTest
{
public void test()
{
//定义两个String对象,并对其赋值
System.String strA="Hello";
System.String strB="World";
String newStr="";
///字符串比较
Console.WriteLine("----------------字符串比较----------------");
//Compare
Console.WriteLine(String.Compare(strA,strB)); //-1
Console.WriteLine(String.Compare(strA,strA)); //0
Console.WriteLine(String.Compare(strB,strA)); //1
//CompareTo
Console.WriteLine(strA.CompareTo(strB)); //-1
//Equals
Console.WriteLine(String.Equals(strA,strB)); //false
Console.WriteLine(strA.Equals(strB)); //false
//==和!=
Console.WriteLine(strA==strB); //false
Console.WriteLine(strA!=strB); //false
//定位子串和字符
Console.WriteLine("----------------定位子串和字符----------------");
//StartWith|EndWith
Console.WriteLine(strA.StartsWith("He")); //true
Console.WriteLine(strA.StartsWith("MM")); //false
Console.WriteLine(strA.EndsWith("lo")); //true
Console.WriteLine(strA.EndsWith("MM")); //false
//Indexof|LastIndexOf
Console.WriteLine(strA.IndexOf('l')); //2
Console.WriteLine(strA.LastIndexOf('l')); //3
//IndexofAny|LastIndexOfAny
char[] anyOf={'H','e','l'};
Console.WriteLine(strA.IndexOfAny(anyOf)); //0
Console.WriteLine(strA.LastIndexOfAny(anyOf)); //3
//格式化字符串
Console.WriteLine("----------------格式化字符串----------------");
//Format
newStr="";
newStr=String.Format("{0},{1}!",strA,strB);
Console.WriteLine(newStr); //Hello,World!
newStr=String.Format("CurrentTime={0:yyyy-MM-dd}",System.DateTime.Now);
Console.WriteLine(newStr); //形如:2006-05-19
//连接字符串
Console.WriteLine("----------------连接字符串----------------");
//Concat
newStr="";
newStr=String.Concat(strA," ",strB);
Console.WriteLine(newStr); //"HelloWorld"
//Join
newStr="";
String[] strArr={strA,strB};
newStr=String.Join("^^",strArr);
Console.WriteLine(newStr); //"Hello^^World"
//+
newStr="";
newStr=strA+strB;
Console.WriteLine(newStr); //"HelloWorld"
//分裂字符串
Console.WriteLine("----------------分裂字符串----------------");
//Split
newStr="Hello^^World";
char[] separator={'^'};
String[] splitStrings=new String[100];
splitStrings=newStr.Split(separator);
int i=0;
while(i<splitStrings.Length)
{
Console.WriteLine("item{0}:{1}",i,splitStrings[i]);
i++;
}
//插入操作
Console.WriteLine("----------------插入操作----------------");
//Insert
newStr="";
newStr=strA.Insert(1,strB);
Console.WriteLine(newStr); //"HWorldello"
//删除操作
Console.WriteLine("----------------删除操作----------------");
//Remove
newStr="";
newStr=strA.Remove(1,3);
Console.WriteLine(newStr); //"Ho"
//Trim
newStr="";
char[] trimChars={'@','#','$',' '};
String strC="@Hello# $";
newStr=strC.Trim(trimChars);
Console.WriteLine(newStr); //"Hello"
//PadLeft
newStr="";
newStr=strA.PadLeft(20,'*');
Console.WriteLine(newStr); //"HWorldello"
//复制操作
Console.WriteLine("----------------复制操作----------------");
//Copy
newStr="";
newStr=String.Copy(strA);
Console.WriteLine(newStr); //"Hello"
//CopyTo
char[] newCharArr=new char[100];
strA.CopyTo(2,newCharArr,0,3);
Console.WriteLine(newCharArr); //"Hel"
//替换操作
Console.WriteLine("----------------替换操作----------------");
//Replace
newStr=strA.Replace("ll","r");
Console.WriteLine(newStr); //Hero
//大小写转换
Console.WriteLine("----------------大小写转换----------------");
//ToUpper|ToLower
newStr=strA.ToUpper();
Console.WriteLine(newStr); //HELLO
newStr=strA.ToLower();
Console.WriteLine(newStr); //hello
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
MainTest t=new MainTest();
t.test();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -