找到 34423 篇文章,关于编程

在 C# 中使用通配符匹配字符串

varma
更新于 2020年6月22日 13:15:59

4K+ 浏览量

常用的通配符是星号 (*)。它代表字符串中零个或多个字符。在下面的例子中,星号用于匹配以 m 开头并以 e 结尾的单词 -@”\bt\S*s\b” 以下是完整的代码 -示例   演示使用 System; using System.Text.RegularExpressions; namespace Demo {    public class Program {       private static void showMatch(string text, string expr) {          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);          }     ... 阅读更多

C# 应用程序中的退出方法

radhakrishna
更新于 2020年6月22日 13:16:20

11K+ 浏览量

Environment.Exit() 方法Environment.Exit() 方法终止进程并向操作系统返回一个退出代码 -Environment.Exit(exitCode);使用 exitCode 为 0 (零) 表示进程成功完成。使用 exitCode 为非零数字表示错误,例如 -Environment.Exit(1) - 返回值 1 表示您想要的的文件不存在Environment.Exit(2) - 返回值 2 表示文件格式不正确。System.Windows.Forms.Application.ExitThread( )要关闭 Windows 窗体中的子应用程序或当前线程,请使用System.Windows.Forms.Application.ExitThread( )。private void buttonClose_Click(object sender, EventArgs eventArgs) {    System.Windows.Forms.Application.ExitThread( ); }阅读更多

如何在 C# 中将列表集合转换为数组?

vanithasree
更新于 2020年6月22日 13:05:09

908 浏览量

首先,设置一个列表集合 -List < string > myList = new List < string > (); myList.Add("RedHat"); myList.Add("Ubuntu");现在,使用 ToArray() 将列表转换为数组 -string[] str = myList.ToArray();以下是完整的代码 -示例   演示使用 System; using System.Collections.Generic; public class Program {    public static void Main() {       List < string > myList = new List < string > ();       myList.Add("RedHat");       myList.Add("Ubuntu");       Console.WriteLine("List...");       foreach(string value in myList) {          Console.WriteLine(value);       } ... 阅读更多

如何在 C# 中将值设置为一维数组中指定位置的元素

radhakrishna
更新于 2020年6月22日 13:05:58

323 浏览量

首先,设置数组 -int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};现在,假设您需要设置位置 1 的元素 -p[2] = 77;让我们看看完整的代码 -示例   演示使用 System; namespace Program {    public class Demo {       public static void Main(string[] args) {          int[] p = new int[] {55, 66, 88, 99, 111, 122, 133};          int j;          Console.WriteLine("初始数组......");          for (j = 0; j < p.Length; j++ ) { ... 阅读更多

在 C# 中交换字符串的字符

Arjun Thakur
更新于 2020年6月22日 13:06:49

2K+ 浏览量

要交换字符串的字符,请使用 Select 方法。首先,假设我们的字符串是 -string str = "PQRQP";现在,您需要交换每个出现的 P 与 Q,以及 Q 与 P -str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();上述代码替换了字符。让我们看看完整的代码 -示例   演示使用 System; using System.Linq; public class Program {    public static void Main() {       string str = "PQRQP";       var res= str.Select(a=> a == 'P' ? 'Q' : (a=='Q' ? 'P' : a)).ToArray();       str = new String(res);       Console.WriteLine(str);    } }输出QPRPQ

在 C# 中使用 ToString 进行字符串格式化

varun
更新于 2020年6月22日 13:06:18

635 浏览量

要格式化字符串,首先设置值 -int value = 55;现在要格式化整数,使用 ToString,假设我们需要将其设置为三位 -value.ToString("000");以下是完整的代码 -示例   演示使用 System; public class Program {    public static void Main() {       int value = 55;       string res = value.ToString("000");       Console.WriteLine(res);    } }输出055

在 C# 中进行字符串格式化以在右侧添加填充

vanithasree
更新于 2020年6月22日 13:07:37

222 浏览量

要在字符串的右侧添加填充 -const string format = "{0,10}";现在将其添加到字符串中 -string str1 = string.Format(format, "Marks","Subject");让我们看看完整的代码 -示例   演示使用 System; public class Program {    public static void Main() {       // 设置右填充       const string format = "{0,10}";       string str1 = string.Format(format, "Marks","Subject");       string str2 = string.Format(format, "95","Maths");       Console.WriteLine(str1);       Console.WriteLine(str2);    } }输出Marks 95

在 C# 中使用 % 进行字符串格式化

Ankith Reddy
更新于 2020年6月22日 13:07:13

280 浏览量

使用 String.Format 使用 % 格式化字符串。C# 中的 String.Format 格式控制还包括百分比 (%)。这将值乘以 100 并附加百分号。假设我们的值为 -double val = .322;现在,使用 String.Format 和格式 -string.Format("string = {0:0.0%}", val);以下是一个示例 -示例   演示使用 System; public class Program {    public static void Main() {       double val = .322;       string res = string.Format("string = {0:0.0%}", val);       Console.WriteLine(res);    } }输出string = 32.2%

在 C# 中进行字符串格式化以添加填充

seetha
更新于 2020年6月22日 13:08:09

916 浏览量

使用 C#,您可以轻松地格式化内容并向其添加填充。要添加填充 -const string format = "{0,-5} {1,5}";现在,将填充添加到字符串中 -string str1 = string.Format(format, "Rank","Student"); string str2 = string.Format(format, "2","Tom");让我们看看完整的代码 -示例   演示使用 System; using System.Collections.Generic; public class Program {    public static void Main() {       // 设置填充       const string format = "{0,-5} {1,5}";       string str1 = string.Format(format, "Rank","Student");       string str2 = string.Format(format, "2","Tom");       Console.WriteLine(str1);       Console.WriteLine(str2);    } }输出Rank Student 2 Tom

在 C# 中进行字符串切片以旋转字符串

George John
更新于 2020年6月22日 13:08:40

347 浏览量

假设我们的字符串是 -var str = "welcome";使用 substring() 方法和以下方法,如果您只想旋转某些字符。在这里,我们只旋转 2 个字符 -var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);以下是完整的代码 -示例   演示使用 System; public class Program {    public static void Main() {       var str = "welcome";       Console.WriteLine("原始字符串 = "+str);       var res = str.Substring(1, str.Length - 1) + str.Substring(0, 2);       Console.WriteLine("旋转字符串中的两个字符: "+res);    } }输出原始字符串 = welcome 旋转字符串中的两个字符: elcomewe

广告
© . All rights reserved.