Synchronization of old projects
Part1
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,68 @@
|
||||
using System;
|
||||
namespace lesson11
|
||||
{
|
||||
class program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Console.WriteLine("字符串拼接");
|
||||
Console.WriteLine();
|
||||
|
||||
#region 知识点一字符串拼接方式1
|
||||
//算数运算符只是用来数值运算
|
||||
//而string不能来做数值运算
|
||||
//但是可以用来做字符串拼接
|
||||
Console.WriteLine("拼接方式一加号拼接:");
|
||||
//字符串拼接的方式1:使用加号运算符
|
||||
string str = "123";
|
||||
str = str + "456"; //字符串拼接
|
||||
Console.WriteLine(str);
|
||||
|
||||
str = str + 1;//1虽然没有加双引号,但它会被自动转换为字符串
|
||||
Console.WriteLine(str);
|
||||
|
||||
//如果需要加上数值,可以先转换为数值类型,运算完成后再转换为字符串
|
||||
str = (int.Parse(str) + Convert.ToInt32("1")).ToString();
|
||||
Console.WriteLine(str);
|
||||
|
||||
//复合运算符也可以用来拼接字符串
|
||||
str = "123";
|
||||
str += "456" + 7 + true;
|
||||
Console.WriteLine(str);
|
||||
|
||||
//但是要注意加号运算符拼接时,后面全是数值类型
|
||||
str = "123";
|
||||
str += 4 + 5;//这里因为运算规则,会先看右边全是数值类型,所以是以数值类型先运算完后再转换为字符串拼接
|
||||
Console.WriteLine(str);
|
||||
str = "123";
|
||||
str += 1 + 2 + "" + 3 + 4;//这里会先运算1+2的结果再把3和4当作字符串拼接上去
|
||||
Console.WriteLine(str);
|
||||
|
||||
//如果后方只拼接数值类型,但又不想被当作数值类型运算,可以直接套双引号或加空双引号""来避免这种问题
|
||||
str = "123";
|
||||
str += "" + 4 + 5;
|
||||
Console.WriteLine(str);
|
||||
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点二字符串拼接方式2
|
||||
//固定语法
|
||||
//string.Format("待拼接的字符串", 内容1, 内容2, ...);
|
||||
//拼接内容中的固定规则
|
||||
//想要被拼接的内容用占位符{}来表示
|
||||
//eg: "拼接内容{0},内容{1},内容{2}"
|
||||
string str2 = string.Format("我是{0},今年{1}岁,我想要{2}", "ZJ", 18, "天天开心");
|
||||
Console.WriteLine(str2);
|
||||
|
||||
Console.WriteLine();
|
||||
#endregion
|
||||
|
||||
#region 知识点三控制台打印
|
||||
//和方法二类似
|
||||
Console.WriteLine("A{0},B{1},C{2}",1,2,3);
|
||||
#endregion
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user