C#开发中,掌握核心应用函数的使用是提升编码效率的关键,这些函数覆盖了日常开发的多个高频场景,下面通过具体实例逐一讲解。
字符串处理相关函数
字符串操作是开发中最常见的需求,string类提供了大量实用函数,以下是几个高频使用的实例。
判断字符串是否为空或空白
使用string.IsNullOrWhiteSpace函数可以同时判断字符串为null、空字符串或者仅包含空白字符的情况,比传统的IsNullOrEmpty更实用。
using System;
class StringDemo
{
static void Main()
{
string str1 = null;
string str2 = "";
string str3 = " ";
string str4 = "hello";
Console.WriteLine($"str1是否为空或空白: {string.IsNullOrWhiteSpace(str1)}");
Console.WriteLine($"str2是否为空或空白: {string.IsNullOrWhiteSpace(str2)}");
Console.WriteLine($"str3是否为空或空白: {string.IsNullOrWhiteSpace(str3)}");
Console.WriteLine($"str4是否为空或空白: {string.IsNullOrWhiteSpace(str4)}");
}
}
字符串分割与拼接
Split函数可以按指定分隔符拆分字符串,Join函数可以将集合元素拼接成字符串,两者常搭配使用。
using System;
using System.Linq;
class SplitJoinDemo
{
static void Main()
{
string content = "apple,banana,orange,grape";
// 按逗号分割字符串
string[] fruits = content.Split(',');
Console.WriteLine("分割后的水果列表:");
foreach (var fruit in fruits)
{
Console.WriteLine(fruit);
}
// 拼接字符串数组,用|分隔
string newContent = string.Join("|", fruits);
Console.WriteLine($"拼接后的字符串: {newContent}");
}
}
数值计算相关函数
数值处理相关的函数多位于Math类中,以下是几个常用实例。
取整与最值计算
Math.Floor向下取整,Math.Ceiling向上取整,Math.Max和Math.Min分别用于取最大值和最小值。
using System;
class MathDemo
{
static void Main()
{
double num1 = 3.7;
double num2 = 2.3;
Console.WriteLine($"3.7向下取整: {Math.Floor(num1)}");
Console.WriteLine($"3.7向上取整: {Math.Ceiling(num1)}");
Console.WriteLine($"3.7和2.3的最大值: {Math.Max(num1, num2)}");
Console.WriteLine($"3.7和2.3的最小值: {Math.Min(num1, num2)}");
// 计算绝对值
int num3 = -15;
Console.WriteLine($"-15的绝对值: {Math.Abs(num3)}");
}
}
集合操作相关函数
List集合是C#中常用的集合类型,其内置的多个函数可以简化集合操作。
查找与筛选集合元素
Find函数可以查找符合条件的第一个元素,FindAll可以筛选所有符合条件的元素,Exists用于判断是否存在符合条件的元素。
using System;
using System.Collections.Generic;
class ListDemo
{
static void Main()
{
List<int> numbers = new List<int> { 1, 3, 5, 7, 8, 10, 12 };
// 查找第一个大于5的元素
int firstGreater = numbers.Find(x => x > 5);
Console.WriteLine($"第一个大于5的元素: {firstGreater}");
// 筛选所有偶数
List<int> evenNumbers = numbers.FindAll(x => x % 2 == 0);
Console.WriteLine("所有偶数:");
evenNumbers.ForEach(x => Console.WriteLine(x));
// 判断是否存在大于10的元素
bool hasGreaterTen = numbers.Exists(x => x > 10);
Console.WriteLine($"是否存在大于10的元素: {hasGreaterTen}");
}
}
文件读写相关函数
文件操作常用File类的静态函数,无需创建实例即可完成常见的文件读写任务。
读取与写入文本文件
File.ReadAllText可以一次性读取文本文件全部内容,File.WriteAllText可以将内容写入文本文件,File.AppendAllText可以在文件末尾追加内容。
using System;
using System.IO;
class FileDemo
{
static void Main()
{
string filePath = "test.txt";
// 写入内容到文件
File.WriteAllText(filePath, "这是第一行内容n这是第二行内容");
Console.WriteLine("文件写入完成");
// 读取文件全部内容
string content = File.ReadAllText(filePath);
Console.WriteLine("文件内容:");
Console.WriteLine(content);
// 追加内容到文件
File.AppendAllText(filePath, "n这是追加的第三行内容");
Console.WriteLine("内容追加完成");
// 再次读取验证
Console.WriteLine("追加后的文件内容:");
Console.WriteLine(File.ReadAllText(filePath));
}
}
日期时间处理相关函数
DateTime结构体提供了丰富的日期时间处理函数,以下是常用实例。
日期格式转换与计算
ToString可以自定义日期显示格式,AddDays、AddMonths等函数可以进行日期偏移计算。
using System;
class DateTimeDemo
{
static void Main()
{
DateTime now = DateTime.Now;
Console.WriteLine($"当前时间: {now}");
Console.WriteLine($"自定义格式: {now.ToString("yyyy-MM-dd HH:mm:ss")}");
// 日期计算
DateTime tomorrow = now.AddDays(1);
DateTime nextMonth = now.AddMonths(1);
Console.WriteLine($"明天: {tomorrow.ToString("yyyy-MM-dd")}");
Console.WriteLine($"下个月今天: {nextMonth.ToString("yyyy-MM-dd")}");
// 计算两个日期的差值
DateTime targetDate = new DateTime(2024, 12, 31);
TimeSpan diff = targetDate - now;
Console.WriteLine($"距离2024年12月31日还有{diff.Days}天");
}
}