zy89、90_C#中字符串及控制字符串的常用函数
1.字符串
1.1程序代码
static void Main(string[] args)
{
//********************string类*******************
string sentence = "Hello!";
char letter = sentence[0];
Console.WriteLine(letter);
char[] letter1 = { 'C', 'H', 'I', 'C', 'K', 'E', 'N' };
string ss = new string(letter1);
Console.WriteLine(ss);
string ss1 = new string('A', 10);//重复执行10遍
Console.WriteLine(ss1);
Console.WriteLine("The letters in \"Hello\" are:");
for (int i = 0; i < sentence.Length; i++)
{
char letter2 = sentence[i];
Console.Write(letter2 + "\t");
}
//********************复制字符串*******************
// =:
string sentence1 = "就快被融化";
string sentence2 = sentence1;
//sentence1 = "再靠近一点";
Console.WriteLine("\n{0}\n{1}", sentence1, sentence2);
//Copy()
string sentence3 = "再多看一眼";
string sentence4 = string.Copy(sentence3);
Console.WriteLine(sentence3);
Console.WriteLine(sentence4);
//CopyTo()
string source = "someone";
char[] destination = { '*', '*', '*', '*', '*', '*', '*', '*', '*', '*' };
int sourceIndex = 2;
int destinationIndex = 3;
int count = 2;
source.CopyTo(sourceIndex, destination, destinationIndex, count);
for (int i = 0; i < destination.Length; i++)
{
Console.Write(destination[i]);
}
//********************比较字符串*******************
//1.相等运算符==
string string5 = "Hello";
string string6 = "hello";
if (string5 == string6)
{
Console.WriteLine("\nEqual");
}
else
{
Console.WriteLine("\nUnequal");
}
//2.Equsls()
string string7 = "Hello";
if (string7.Equals("Hello"))
{
Console.WriteLine("\nEqual");
}
else
{
Console.WriteLine("\nUnequal");
}
//3.静态Equal()
string string8 = "Dog";
string string9 = "dog";
if (string.Equals(string8, string9))
{
Console.WriteLine("\nEqual");
}
else
{
Console.WriteLine("\nUnequal");
}
//4.CompareTo()
string string10 = "better";
string string11 = "best";
switch (string10.CompareTo(string11))
{
case -1: Console.WriteLine(string10 + "<" + string11); break;
case 0: Console.WriteLine(string10 + "=" + string11); break;
case 1: Console.WriteLine(string10 + ">" + string11); break;
default: Console.WriteLine("********"); break;
}
}
2.控制字符串的常用函数
2.1程序代码
static void Main(string[] args)
{
验证字符串的首尾
//1.StartWith()判断字符串是否以特定的子串开头
string[] strings = { "started", "starting", "ended", "ending" };
for (int i = 0; i < strings.Length; i++)
{
if (strings[i].StartsWith("st"))
{
Console.WriteLine("\"{0}\" starts with \"st\"", strings[i]);
}
}
//2.EndsWith()判断字符串是否以特定的子串结尾
for (int i = 0; i < strings.Length; i++)
{
if (strings[i].EndsWith("ed"))
{
Console.WriteLine("\"{0}\" starts with \"ed\"", strings[i]);
}
}
定位字符或子串
//1.IndexOf()找到指定字符或子串在字符串中第一次出现的位置。如果找不到,返回-1
string sentence = "You love to hear Let her go.";
int index1 = sentence.IndexOf('o', 3);//从第三个字符之后开始找指定元素
int index2 = sentence.IndexOf("You", 20);
Console.WriteLine(index1);
Console.WriteLine(index2);
//2.LastIndexOf()找到指定字符或子串在字符串中最后一次出现的位置。如果找不到,返回-1
int index3 = sentence.IndexOf('o', 6);//从后向前搜索
int index4 = sentence.IndexOf("You", 20,4);//向前搜索4个字符
Console.WriteLine(index3);
Console.WriteLine(index4);
//3.IndexOfAny()同时搜索多个指定的字符,搜索到任意一个停止
char[] searchLetters= { 'e', 'h' };
int index5 = sentence.IndexOfAny(searchLetters);
int index6 = sentence.IndexOfAny(searchLetters,10);//第十个元素开始
int index7 = sentence.IndexOfAny(searchLetters,10,3);
Console.WriteLine(index5);
Console.WriteLine(index6);
Console.WriteLine(index7);
//4.LastIndexOfAny()
int index8 = sentence.LastIndexOfAny(searchLetters);
int index9 = sentence.LastIndexOfAny(searchLetters, 10);
int index10 = sentence.LastIndexOfAny(searchLetters, 10, 3);
Console.WriteLine(index8);
Console.WriteLine(index9);
Console.WriteLine(index10);
///截取子串Substring()
string sub=sentence.Substring(12);//截取12之后的字符串
Console.WriteLine(sub);
string sub1=sentence.Substring(5,4);//在第5个之后截取4个
Console.WriteLine(sub1);
//拆分字符串Split()
char[] seperator = { ' ','o' };//遇到字符断开
string[] words=sentence.Split(seperator);
foreach (string word in words)
{
Console.WriteLine(word);
}
//更改大小写
string string1 = "ok";
string string2=string1.ToUpper();
Console.WriteLine(string2);
string string3 = string2.ToLower();
Console.WriteLine(string3);
//修改字符串
string string4 = "tree";
string string5 = string4.Insert(1, "h");
Console.WriteLine(string5);
string string6 = string4.Replace('e', 'd');
Console.WriteLine(string6);
string string7 = string4.Replace("re", "ffd");
Console.WriteLine(string7);
string string8 = string4.Remove(2);//2以后所有元素
Console.WriteLine(string8);
string string9=string4.Remove(1,2);//1以后的2个字符
Console.WriteLine(string9);
插入格式化变量
int i1 = 360;
int j1 = 60;
string result = string.Format("{0,6}\n+{1,5}\n----------\n{2,6}", i1, j1, i1 + j1);//{0}6个空间
Console.WriteLine(result);
}
原文地址:https://blog.csdn.net/weixin_47149785/article/details/142637523
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!