找到 34423 篇文章,关于编程
2K+ 次浏览
C# 中使用 Equals 方法比较两个 StringBuilder 的内容。以下是我们的两个 StringBuilder: // 第一个 StringBuilder str1 = new StringBuilder(); str1.Append("Tim"); str1.Append("Tom"); str1.Append("Henry"); // 第二个 StringBuilder str2 = new StringBuilder(); str2.Append("John"); str2.Append("David"); str2.Append("Beth"); 现在使用 Equals() 方法比较这两个方法: if (str1.Equals(str2)) { Console.WriteLine("内容相同!"); } 完整的代码如下: 示例 在线演示 using System; using System.Text; class Demo { static void Main() { // 第一个 StringBuilder str1 = new StringBuilder(); str1.Append("Tim"); str1.Append("Tom"); str1.Append("Henry"); ... 阅读更多
602 次浏览
设置一个字符串 −StringBuilder str = new StringBuilder("Fitness is important"); 使用 Replace() 方法替换字符串 −str.Replace("important", "essential"); 使用 StringBuilder 替换字符串的代码如下: 示例 在线演示 using System; using System.Text; class Demo { static void Main() { // 初始字符串 StringBuilder str = new StringBuilder("Fitness is important"); Console.WriteLine(str.ToString()); // 替换 str.Replace("important", "essential"); // 新字符串 Console.WriteLine(str.ToString()); Console.ReadLine(); } } 输出 Fitness is important Fitness is essential
3K+ 次浏览
AppendLine() 方法追加内容并在末尾添加新行。首先,设置 StringBuilder:StringBuilder str = new StringBuilder(); 使用 AppendLine():str.AppendLine("Accessories"); str.AppendLine(); str.AppendLine("Electronics"); 完整的代码如下: 示例 在线演示 using System; using System.Text; class Demo { static void Main() { StringBuilder str = new StringBuilder(); str.AppendLine("Accessories"); str.AppendLine(); str.AppendLine("Electronics"); Console.Write(str); } } 输出 Accessories Electronics
439 次浏览
Append() 方法向 StringBuilder 添加内容。设置一个字符串:StringBuilder str = new StringBuilder(); 现在,循环遍历你想要的元素数量,并使用 Append() 向 StringBuilder 追加内容:for (int j = 0; j < 5; j++) { str.Append(j).Append(" "); } 完整的代码如下: 示例 在线演示 using System; using System.Text; class Program { static void Main() { StringBuilder str = new StringBuilder(); for (int j = 0; j < 5; j++) { str.Append(j).Append(" "); } Console.WriteLine(str); } } 输出 0 1 2 3 4
10K+ 次浏览
使用 Console.Clear() 方法清除屏幕和控制台缓冲区。调用 Clear 方法时,光标会自动滚动到窗口的左上角。在这里,我们清除了屏幕,然后设置了 ForegroundColor 和 BackgroundColor:ConsoleColor newForeColor = ConsoleColor.Blue; ConsoleColor newBackColor = ConsoleColor.Yellow; 完整的代码如下: 示例 在线演示 using System; using System.Collections.Generic; class Program { static void Main() { ConsoleColor foreColor = Console.ForegroundColor; ConsoleColor backColor = Console.BackgroundColor; Console.WriteLine("正在清除屏幕!"); Console.Clear(); ConsoleColor newForeColor = ConsoleColor.Blue; ConsoleColor newBackColor = ConsoleColor.Yellow; } } 输出 正在清除屏幕!
793 次浏览
变量的作用域是指代码区域,指示在何处访问变量。对于一个变量,它具有以下级别: 方法级别 在方法内部声明的变量是局部变量。 类级别 在类内部声明的变量是局部变量,也是类成员变量。让我们来看一个变量作用域的示例: 示例 在线演示 using System; namespace Demo { class Program { public int Divide(int num1, int num2) { // 方法中的局部变量 int result; result = num1 ... 阅读更多
5K+ 次浏览
Pair 类是 KeyValuePair 类,它使用 C# 将一对值存储在一个列表中。声明 KeyValuePair:var myList = new List<string, int>(); 现在,添加一些元素:myList.Add(new KeyValuePair("Laptop", 1)); myList.Add(new KeyValuePair("Desktop System", 2)); myList.Add(new KeyValuePair("Tablet", 3)); myList.Add(new KeyValuePair("Mobile", 4)); myList.Add(new KeyValuePair("E-Book Reader", 5)); myList.Add(new KeyValuePair("LED", 6)); 现在显示 KeyValuePair,如下所示: 示例 在线演示 using System; using System.Collections.Generic; class Program { static void Main() { var myList = new List<KeyValuePair<string, int>>(); // 添加元素 myList.Add(new KeyValuePair("Laptop", 1)); myList.Add(new KeyValuePair("Desktop System", 2)); ... 阅读更多
3K+ 次浏览
对于 C# 中的文件权限,使用 FileIOPermission 类。它控制访问文件和文件夹的能力。以下是文件权限类的属性: 序号 方法及说明 1 AllFiles 获取或设置对所有文件的允许访问权限。 2 AllLocalFiles 获取或设置对所有本地文件的允许访问权限。 以下是文件权限类的方法: 序号 方法及说明 1 AddPathList(FileIOPermissionAccess, String) 此方法将指定文件或目录的访问权限添加到权限的现有状态 2 Copy() 此方法创建并返回当前权限的相同副本。 3 GetType() GetType() 方法获取当前实例的类型。 4 ToXml() 创建权限的 XML 编码 ... 阅读更多
528 次浏览
Hashtable 比 Dictionary 慢。对于强类型集合,Dictionary 集合更快。Hashtable Hashtable 类表示键值对的集合,这些键值对根据键的哈希代码进行组织。它使用键来访问集合中的元素。让我们来看一个示例: 示例 在线演示 using System; using System.Collections; namespace Demo { class Program { static void Main(string[] args) { Hashtable ht = new Hashtable(); ht.Add("E001", "Tom"); ht.Add("E098", "Amit"); ht.Add("E110", "Jack"); ... 阅读更多
1K+ 次浏览
设置一个数组:int[] arr = { 89, 12, 56, 89, }; 现在,创建一个新的 Dictionary:var d = new Dictionary<int, int>(); 使用 dictionary 方法 ContainsKey(),查找数组中的重复元素:foreach(var res in arr) { if (d.ContainsKey(res)) d[res]++; else d[res] = 1; } 完整的代码如下: 示例 在线演示 using System; using System.Collections.Generic; namespace Demo { public class Program { public static void Main(string[] args) { int[] arr = { 89, ... 阅读更多
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP