1、字符串与字符数组间的转换
变成字符数组:ToCharArray()
变成字符串:new string()
string text = "";
char[] chs = text.ToCharArray();//变成字符数组
chs[5]='';//改值
text = new string(chs);//变成字符串
2、使得字符串不区分大小写
转化为小写:ToLower()
转化为大写:ToUpper()
忽略大小写:Equals(course2, StringComparison.OrdinalIgnoreCase)
string course1=Console.ReadLine();
course1 = course1.ToLower();//转化为小写
course1 = course1.ToUpper();//转化为大写
string course2=Console.ReadLine();
bool result = course1.Equals(course2,StringComparison.OrdinalIgnoreCase);//忽略大小写
3、字符串的截取、替换、包含、移除、插入
string name = "";
str = str.Substring(3,4);//从某个地方开始截取,截取多少个
name = name.Replace('', '');//替换
bool result = name.Contains("");//包含
name = name.Remove(3,4);//从某个地方开始移除,移除多少个
name = name.Insert(7, "!");//在某处索引处插入一个字符串
4、字符串的开始与结尾
StartsWith和EndsWith
string str = "烽火佳人佟毓婉周霆琛杜允唐";
bool result = str.StartsWith(""烽火佳人");//判断字符串是否以某个字符串开始的
bool result = str.EndsWith("杜允唐");//判断字符串是否以某个字符串结束的
5、字符串索引
IndexOf
string str = "abcdeffabcdabc";
int index = str.IndexOf("a",1);//找一个字符串,从某个索引开始开始找,找到就返回索引,找不到就返回-1
int index = str.LastIndexOf("a");//找最后一个字符串的索引
找e的位置
string st = "aefegecebdfebdelelelsleoeooer";
int count = 0;
int index = st.IndexOf("e");
while (index != -1)
{
count++;
Console.WriteLine("第{0}个e的索引为索:{1}",count,index);
index = st.IndexOf("e", index + 1);
}
Console.WriteLine();
Console.ReadKey();
6、字符串的反序输出
(1)字母的反序输出 abc->cba
string text = Console.ReadLine();
for (int i = text.Length-1; i >= 0; i--)
{
Console.Write(text[i]);
}
(2)单词的反序输出how are you->you are how
string text = Console.ReadLine();
string []strs=text.Split(' ');
string st="";
for(int i=strs.Length-1;i>=1;i--)
{
st+=strs[i]+" ";
}
Console.WriteLine(st+strs[0]);
Console.ReadKey();
Split:分割字符串
如:string email = “abc@163.com”;
string[] sts = email.Split(‘@’);
就是把abc@163.com分为abc和163.con
7、加”|”
string[] names = {"佟毓婉","周霆琛","杜允唐","闵茹","红羽"};
string st = string.Join("|", names);