找到 34423 篇文章 关于编程

如何在 C# 中使用字符串分隔符分割字符串?

Samual Sam
更新于 2020年6月23日 14:24:32

741 次浏览

分隔符是您在以下字符串中看到的逗号。string str = "Welcome, to, New York";现在分别设置分隔符。char[] newDelimiter = new char[] { ', ' };使用 Split() 方法分割字符串,将分隔符作为参数。str.Split(newDelimiter, StringSplitOptions.None);要使用字符串分隔符分割字符串,请尝试运行以下代码 - 示例使用 System; class Program { static void Main() { string str = "Welcome, to, New York"; char[] newDelimiter = new char[] { ', ' }; string[] arr = str.Split(newDelimiter, StringSplitOptions.None); ... 阅读更多

如何在 C# 中使用正则表达式分割字符串?

George John
更新于 2020年6月23日 14:24:54

1K+ 次浏览

要使用正则表达式分割字符串,请使用 Regex.split。假设我们的字符串是 - string str = "Hello\rWorld";现在使用 Regex.split 分割字符串,如下所示 - tring[] res = Regex.Split(str, "\r");以下是使用 C# 中的正则表达式分割字符串的完整代码。示例使用 System; using System.Text.RegularExpressions; class Demo { static void Main() { string str = "Hello\rWorld"; string[] res = Regex.Split(str, "\r"); foreach (string word in res) { Console.WriteLine(word); } } }输出Hello World

如何在 C# 中将字符串分割成字符串数组的元素?

karthikeya Boyini
更新于 2020年6月23日 14:25:16

1K+ 次浏览

设置要分割的字符串。string str = "Hello World!";使用 split() 方法将字符串分割成单独的元素。string[] res = str.Split(' ');以下是将字符串分割成 C# 中字符串数组元素的完整代码。示例使用 System; class Demo { static void Main() { string str = "Hello World!"; string[] res = str.Split(' '); Console.WriteLine("单独元素:"); foreach (string words in res) { Console.WriteLine(words); } } }输出单独元素:Hello World!

如何在 C# 中根据字典的值对字典列表进行排序?

Samual Sam
更新于 2020年6月23日 14:26:29

402 次浏览

设置带有键和值的字典列表。var d = new Dictionary(); d.Add("Zack", 0); d.Add("Akon", 3); d.Add("Jack", 2); d.Add("Tom", 1);获取并排序键。var val = d.Keys.ToList(); val.Sort();您可以尝试运行以下代码来根据值对字典列表进行排序。示例使用 System; using System.Collections.Generic; using System.Linq; class Demo { static void Main() { var d = new Dictionary(); d.Add("Zack", 0); d.Add("Akon", 3); d.Add("Jack", 2); d.Add("Tom", 1); // 获取键并排序。 var val ... 阅读更多

如何在 C# 中访问锯齿数组?

Ankith Reddy
更新于 2020年6月23日 14:26:01

110 次浏览

锯齿数组是数组的数组。您可以声明一个名为 scores 的 int 类型锯齿数组。int [][] points;现在让我们看看如何初始化它。int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, 80}, new int[]{ 60, 70 }};访问锯齿数组元素如下 - int x = points[0][1];以下是如何在 C# 中访问锯齿数组的完整示例。示例使用 System; namespace ArrayApplication { class MyArray { static void Main(string[] args) { int[][] points = new int[][]{new int[]{10, 5}, new int[]{30, 40}, new int[]{70, ... 阅读更多

如何在 C# 中从列表中选择一个随机元素?

Arjun Thakur
更新于 2020年6月23日 14:12:45

42K+ 次浏览

首先,在 C# 中设置一个列表。var list = new List{ "one","two","three","four"};现在获取元素的数量并随机显示。int index = random.Next(list.Count); Console.WriteLine(list[index]);要从 C# 列表中选择一个随机元素,请尝试运行以下代码 - 示例使用 System; using System.Collections.Generic; namespace Demo { class Program { static void Main(string[] args) { var random = new Random(); var list = new List{ "one","two","three","four"}; int index = random.Next(list.Count); Console.WriteLine(list[index]); } } }输出three

如何在 C# 中使用 Console 类的 WriteLine() 方法?

Chandu yadav
更新于 2020年6月23日 14:13:17

296 次浏览

WriteLine() 是在 System 命名空间中定义的 Console 类的方 法此语句导致消息“Welcome!”显示在屏幕上,如下所示 - 示例使用 System; namespace Demo { class Test { static void Main(string[] args) { Console.WriteLine("Welcome!"); Console.ReadKey(); } } }输出Welcome!要使用 Console.WriteLine 显示字符数组。示例使用 System; namespace Demo { class Test { static void Main(string[] args) { char[] arr = new char[] { 'W', 'e'}; Console.WriteLine(arr); Console.ReadKey(); } } }输出We

如何在 C# 中使用 XmlSerializer?

karthikeya Boyini
更新于 2019年7月30日 22:30:23

491 次浏览

序列化/反序列化允许通过发送和接收数据与另一个应用程序进行通信。使用 XmlSerializer,您可以控制对象如何编码为 XML。要执行 XML 序列化,您需要以下两个类 - StreamWriter 类 XmlSerializer 类使用 StreamWriter 和要序列化的对象的参数调用 Serialize 方法。string myPath = "new.xml"; XmlSerializer s = new XmlSerializer(settings.GetType()); StreamWriter streamWriter = new StreamWriter(myPath); s.Serialize(streamWriter, settings);一个名为“new.xml”的 XML 文件可见。现在反序列化。MySettings mySettings = new MySettings(); string myPath = "new.xml"; XmlSerializer ... 阅读更多

如何在 C# 中使用 Try/catch 块?

Samual Sam
更新于 2020年6月23日 14:14:50

2K+ 次浏览

异常提供了一种将控制从程序的一个部分转移到另一个部分的方法。C# 异常处理基于四个关键字:try、catch、finally 和 throw。try - try 块标识一个为其激活特定异常的代码块。它后面是一个或多个 catch 块。catch - 程序使用异常处理程序在一个程序中捕获异常,您可以在该处处理问题。catch 关键字表示捕获异常。以下是如何在 C# 中使用 try、catch 和 finally 的示例。示例使用 ... 阅读更多

如何在 C# 中使用数组的 ToString() 方法?

George John
更新于 2020年6月23日 14:15:29

345 次浏览

ToString() 方法返回一个表示当前对象的字符串。在下面的示例中,我们将 ToString() 方法与另一个 Array 类方法 arr.GetLowerBound(0).ToString() 一起使用。示例 在线演示using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace lower {    class Program {       static void Main(string[] args) {          Array arr = Array.CreateInstance(typeof(String), 3);          arr.SetValue("One", 0);          arr.SetValue("Two", 1);          Console.WriteLine("下界 {0}",arr.GetLowerBound(0).ToString());          Console.ReadLine();       }    } }输出下界 0

广告
© . All rights reserved.