字符串比较
namespace demo1
{
internal class progrm
{
static void Main(string[] args)
{
string str = "hello world";
Console.WriteLine("大写字符{0}",str.ToUpper());
Console.WriteLine("小写字符{0}",str.ToLower());
Console.WriteLine("字符长度{0}", str.Length);
string a = "hello";
string b = "world";
int c=string.Compare(a,b);
if (c > 0)
{
Console.WriteLine("c={0} a>b",c);
}
else
{
Console.WriteLine("c={0},a<b",c);
}
int d=a.CompareTo(b);
Console.WriteLine(d);
bool e=a.Equals(b);
Console.WriteLine(e);
bool f=String.Equals(a,b);
Console.WriteLine(f);
}
}
}
字符串格式化
namespace demo1
{
internal class progrm
{
static void Main(string[] args)
{
for (int i = 0;i<10;i++)
{
string str = String.Format("{0:E}",i);
Console.WriteLine(str);
}
for (int i = 0; i < 10; i++)
{
string str = String.Format("{0:C}", i);
Console.WriteLine(str);
}
for (int i = 0; i < 10; i++)
{
string str = String.Format("{0:P}", i);
Console.WriteLine(str);
}
for (int i = 0; i < 10; i++)
{
string str = String.Format("{0:X}", i);
Console.WriteLine(str);
}
}
}
}
字符串常用操作
namespace demo1
{
internal class progrm
{
static void Main(string[] args)
{
Console.WriteLine("字符串截取");
string str = "hello world";
string s1=str.Substring(1,8);
Console.WriteLine(s1);
Console.WriteLine("字符串的分割");
string[] s2=str.Split(' ');
foreach(string i in s2)
{
Console.WriteLine(i);
}
Console.WriteLine("字符串合并");
string str1 = "hello";
string str2 = "world";
Console.WriteLine(str1+str2);
Console.WriteLine(string.Join("_", str1,str2));
Console.WriteLine(string.Concat(str1,str2));
Console.WriteLine("字符串的插入");
string str3 = "孙";
string str4 = str1.Insert(str1.Length, str3);
Console.WriteLine(str4);
Console.WriteLine("字符串的填充");
string str5=str1.PadRight(20,' ');
Console.WriteLine(str5);
Console.WriteLine(str5.Length);
Console.WriteLine("字符串的删除");
string str6=str1.PadRight(20,'h');
string str7=str6.Remove(5);
Console.WriteLine(str7);
string str8 = " hello world ";
Console.WriteLine(str8.Trim());
Console.WriteLine(str8.TrimStart());
Console.WriteLine(str8.TrimEnd());
string str9;
str9 = string.Copy(str8);
Console.WriteLine(str9);
char[] str10=new char[10];
str8.CopyTo(0, str10,0,1);
Console.WriteLine(str10);
string oldstr = "www.hao123";
Console.WriteLine("替换前的字符串{0}",oldstr);
string newstr = oldstr.Replace('w', 'W');
Console.WriteLine("替换后的{0}", newstr);
string str12 = "good morning,everyone";
Console.WriteLine(str12);
int a=str12.IndexOf("g");
Console.WriteLine(a);
int b =str12.LastIndexOf("o");
Console.WriteLine(b);
}
}
}