为编程找到了 34423 篇文章

如何在中使用字符串分隔符拆分字符串?

Samual Sam
更新时间:2020 年 6 月 23 日,星期二 下午 2:24:32

浏览量:741

分隔符是您可以在以下字符串中看到的逗号。string str = "Welcome, to, New York";现在分别设置分隔符。char[] newDelimiter = new char[] { ', ' };使用 Split() 方法拆分字符串,同时考虑分隔符作为参数。str.Split(newDelimiter, StringSplitOptions.None);要使用字符串分隔符拆分字符串,请尝试运行以下代码示例:实时演示系统;类程序 {    static void Main() {       string str = "Welcome, to, New York";       char[] newDelimiter = new char[] { ', ' };       string[] arr = str.Split(newDelimiter, StringSplitOptions.None);     ... 阅读更多

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

George John
更新时间:2020 年 6 月 23 日,星期二 下午 2:24:54

浏览量:1000+

要使用正则表达式分割字符串,请使用 Regex.split。假设我们的字符串为 −字符串 str = “Hello\rWorld”;现在使用 Regex.split 按如下所示分割字符串 - tring[] res = Regex.Split(str, “\r”);以下是使用 C# 中的正则表达式分割字符串的完整代码。示例 实时演示系统;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 更新

浏览量:1000+

设置要分割的字符串。string str = “Hello World!”;使用 split() 方法将字符串分割为单独的元素。string[] res = str.Split(‘ ’);以下是将字符串分割为字符串数组元素的 C# 完整代码。示例 实时演示系统;class Demo {    static void Main() {       string str = “Hello World!”;       string[] res = str.Split(‘ ’);       Console.WriteLine(“Separate elements:”);       foreach (string words in res) {          Console.WriteLine(words);       }    } }输出 Separate elements: 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();您可以尝试运行以下代码,按字典的值对字典列表进行排序。示例 实时演示系统;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# 中访问锯齿数组的完整示例。示例using 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# 中选择列表中的随机元素,请尝试运行以下代码:示例 在线演示例子系统;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!”,如下所示:示例 在线演示例子系统;namespace Demo {  class Test {   static void Main(string[] args) {      Console.WriteLine("Welcome!");      Console.ReadKey();   }  } }输出 Welcome!使用 Console.WriteLine 来显示字符数组。示例 在线演示例子系统;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 的参数和要序列化的对象调用序列化方法。string myPath = "new.xml";XmlSerializer s = new XmlSerializer(settings.GetType());StreamWriter streamWriter = new StreamWriter(myPath);s.Serialize(streamWriter, settings);可见 XML 文件名为“new.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() 方法用到了另一个数组类方法。arr.GetLowerBound(0).ToString()示例 在线示例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.