添加Lesson23
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Lesson23_面向对象相关_String</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,96 @@
|
||||
namespace Lesson23_面向对象相关_String
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
#region 知识点一 字符串指定位置获取
|
||||
//string是一个密封类,不能被继承
|
||||
//字符串的本质是一个char数组
|
||||
string str = "123";
|
||||
Console.WriteLine(str[0]);//一个类能通过中括号访问,那么这个类就实现了索引器
|
||||
|
||||
//转换为char数组
|
||||
char[] chars = str.ToCharArray();
|
||||
Console.WriteLine(chars[1]);
|
||||
for (int i = 0; i < chars.Length; i++)
|
||||
{
|
||||
Console.Write(chars[i]);
|
||||
}
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点二 字符串拼接
|
||||
str = string.Format("{0},{1}", 1, 2);
|
||||
Console.WriteLine(str);
|
||||
str = "123" + "456";
|
||||
Console.WriteLine(str);
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点三 正向查找字符位置
|
||||
str = "abcdefg";
|
||||
int index = str.IndexOf("b");
|
||||
Console.WriteLine(index);
|
||||
index = str.IndexOf("noindex");
|
||||
Console.WriteLine(index);//找不到返回-1
|
||||
|
||||
#endregion
|
||||
|
||||
#region 知识点四 反向查找指定字符串位置
|
||||
str = "123321";
|
||||
index = str.IndexOf("2");//正向查找第一个2
|
||||
Console.WriteLine(index);
|
||||
index = str.LastIndexOf("2");
|
||||
Console.WriteLine(index);//反向查找第一个2
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点五 移除指定位置后的字符
|
||||
str = "1234567890";
|
||||
Console.WriteLine(str);
|
||||
str = str.Remove(5);//移除指定位置后的字符
|
||||
Console.WriteLine(str);
|
||||
|
||||
str = str.Remove(1, 3);//从指定位置开始移除指定个数的字符
|
||||
//从第1个位置往后移除3个字符
|
||||
Console.WriteLine(str);
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点六 替换指定字符串
|
||||
str = "abcdefg";
|
||||
str = str.Replace("cd", "CD");
|
||||
Console.WriteLine(str);
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点七 大小写转换
|
||||
str = "abcDEF";
|
||||
str = str.ToUpper();
|
||||
Console.WriteLine(str);
|
||||
str = str.ToLower();
|
||||
Console.WriteLine(str);
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点八 字符串截取
|
||||
str = "1234567890";
|
||||
str = str.Substring(3);//从指定位置开始截取到字符串末尾
|
||||
Console.WriteLine(str);
|
||||
str = "1234567890";
|
||||
str = str.Substring(3,5);//从指定位置开始截取指定个数的字符
|
||||
Console.WriteLine(str);
|
||||
#endregion
|
||||
|
||||
#region 知识点九 字符串分割
|
||||
str = "1,2,3,4,5,6";
|
||||
string[] strArray = str.Split(',');//根据指定字符分割字符串,返回字符串数组
|
||||
foreach (var item in strArray)
|
||||
{
|
||||
Console.WriteLine(item);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user