DAY 1 SW

Console.WriteLine 方法打印此消息。 Console 是表示控制台窗口的类型。 WriteLine 是 Console 类型的方法,负责将文本行打印到文本控制台。

string 用于声明变量

string aFriend = "Bill";
如果在字符串的左引号前添加 $,则可以在大括号之间的字符串内包括变量
Console.WriteLine("Hello " + aFriend);
Console.WriteLine($"Hello {aFriend}");
 你可以使用 Length 得出字符串的长度。 Length 是字符串属性,可返回字符串中的字符数。
Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");
假设字符串具有你不想显示的前导或尾随空格。 你希望剪裁字符串中的空格。
Trim 方法及相关方法 TrimStart 和 TrimEnd 将完成这项工作。 你只需使用这些方法即可删除前导和尾随空格。
string greeting = "     hello world!    ";
Console.WriteLine($"[{greeting}]");
string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");
trimmedGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");
trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");
方括号 [] 帮助可视化 Trim、TrimStart 和 TrimEnd 方法的作用。 方括号出现在空格开始和结束的位置。
控制字符串的方法返回的是新字符串对象,而不是就地进行修改。 可以看到,对任何 Trim 方法的所有调用都是返回新字符串,而不是更改原始消息。
Replace 方法需要使用两个参数。 这两个字符串用括号括住。 第一个字符串是要搜索的文本。 第二个字符串是替换后的文本。
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
还有两个实用方法,可以将字符串设为全部大写或小写。
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
搜索和替换操作的另一方面是,查找字符串中的文本。 可以使用 Contains 方法进行搜索。 此方法可确定字符串是否包含子字符串。Contains 方法返回布尔值,指明是否找到了要搜索的字符串。 布尔变量存储 truefalse 值。 显示为文本输出时,它们为大写:分别为 True 和 False。
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));
有两个类似方法 StartsWith 和 EndsWith,也可以在字符串中搜索子字符串。 这些方法搜索字符串开头或结尾的子字符串。不能不略空格。
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.StartsWith("You"));
Console.WriteLine(songLyrics.StartsWith("goodbye"));

Console.WriteLine(songLyrics.EndsWith("hello"));
Console.WriteLine(songLyrics.EndsWith("goodbye"));
C# 整数类型不同于数学上的整数的另一点是,int 类型有最小限值和最大限值。 
int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");
decimal 类型的范围较小,但精度高于 doubledouble a = 1.0;double b = 3.0;Console.WriteLine(a / b);
decimal c = 1.0M;decimal d = 3.0M;Console.WriteLine(c / d);
数字中的 M 后缀指明了常数应如何使用 decimal 类型。 否则,编译器假定为 double 类型。
.NET 包含 PI 常数 Math.PI,可用于相应的值计算。 与 System.Math 命名空间中声明的所有常量一样,Math.PI 也是 double 值。
float
±1.5 x 1045 至 ±3.4 x 1038
大约 6-9 位数字
4 个字节
System.Single
double
±5.0 × 10324 到 ±1.7 × 10308
大约 15-17 位数字
8 个字节
System.Double
decimal
±1.0 x 10-28 至 ±7.9228 x 1028
28-2916 个字节
System.Decimal
不带后缀的文本或带有 d 或 D 后缀的文本的类型为 double
带有 f 或 F 后缀的文本的类型为 float
带有 m 或 M 后缀的文本的类型为 decimal

在这里插入图片描述

char 类型关键字是 .NET System.Char 结构类型的别名,它表示 Unicode UTF-16 字符。
char 类型的默认值为 \0,即 U+0000char 类型支持比较、相等、增量和减量运算符。 此外,对于 char 操作数,算数和逻辑位运算符对相应的字符代码执行操作,并得出 int 类型的结果。
字符串类型将文本表示为 char 值的序列。
int counter = 0;
while (counter < 10)
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
}

int counter = 0;
do
{
  Console.WriteLine($"Hello World! The counter is {counter}");
  counter++;
} while (counter < 10);
for(int counter = 0; counter < 10 ; counter ++){    Console.WriteLine($"hello world!the counter is {counter}");}
 IndexOf 方法可搜索项,并返回此项的索引。 如果项不在列表中,IndexOf 将返回 -1var index = names.IndexOf("Felipe");
if (index != -1)
Console.WriteLine($"The name {names[index]} is at index {index}");
var notFound = names.IndexOf("Not Found");
Console.WriteLine($"When an item is not found, IndexOf returns {notFound}");
还可以对列表中的项进行排序。 Sort 方法按正常顺序(如果是字符串则按字母顺序)对列表中的所有项进行排序。
names.Sort();
foreach (var name in names){
Console.WriteLine($"Hello {name.ToUpper()}!");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值