using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace String加深
{
class Program
{
static void Main(string[] args)
{
//StartsWith()
string []strings={"spring","split","summer","seeking"};
foreach(string str in strings )
{
if(str.StartsWith("sp"))
{
Console.WriteLine("{0} StartsWith sp",str);
}
}
//EndsWith()
foreach (string str in strings)
{
if (str.EndsWith("ing"))
{
Console.WriteLine("{0} EndsWith ing", str);
}
}
//IndexOf()
string sentence = "I love you,do you know";
int index1 = sentence.IndexOf("l");
int index2 = sentence.IndexOf('l',2);//从数组下标为2的元素开始找,包括这个元素
Console.WriteLine("{0},{1}", index1, index2);//输出2,2
//LastIndexOf
int index3 = sentence.LastIndexOf('o');
int index4 = sentence.LastIndexOf('o', 3);//从下标0到下标3中的元素中,从3开始向0找元素
Console.WriteLine("{0},{1}", index3, index4);//输出20,3
//IndeOfAny
char[] s = { 'o', 'l' };
int index5 = sentence.IndexOfAny(s);
Console.WriteLine("{0}", index5);//输出2,因为先找到l
//Substring()
string sentences = sentence.Substring(5);
Console.WriteLine(sentences);//输出e you,do you know
//Split()
char[] separtor = { ' ' };
string[] words = sentence.Split(separtor);
//ToUpper()
string sentencess = sentence.ToUpper();
string sentencesss = sentence.ToLower();
}
}
}
C# string 加深
最新推荐文章于 2021-10-29 16:18:28 发布
本文详细介绍了使用C#进行字符串操作的方法,包括如何使用StartsWith()判断字符串开头、EndsWith()判断字符串结尾、IndexOf()和LastIndexOf()查找字符位置、IndexOfAny()查找多个字符中的任意一个、Substring()截取子字符串以及Split()分割字符串等实用技巧。
867

被折叠的 条评论
为什么被折叠?



