找到 34423 篇文章,关于编程

C# 中的 Environment.NewLine

Arjun Thakur
更新于 2020-06-22 15:02:36

2K+ 次查看

C# 中的 Environment.NewLine 用于添加换行符。在单词之间设置换行符:str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";以下代码示例:示例   在线演示using System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "This is demo text!" + Environment.NewLine + "This is demo text on next line!";          Console.Write(str);       }    } }输出This is demo text! This is demo text on next line!

C# 程序:获取 C# 中的文件名

Chandu yadav
更新于 2020-06-22 15:03:07

577 次查看

在字符串中设置路径名:string myPath = "D:ew\quiz.txt";现在,使用 GetFileName() 方法获取文件名:Path.GetFileName(myPath)以下为完整代码:示例   在线演示using System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          // 获取扩展名          Console.WriteLine("扩展名: "+Path.GetExtension(myPath));          // 获取路径          Console.WriteLine("文件路径: "+Path.GetFileName(myPath));       }    } }输出扩展名: .txt 文件路径: D:ew\quiz.txt

C# 程序:获取 C# 中文件的扩展名

Arjun Thakur
更新于 2020-06-22 15:03:30

248 次查看

在 C# 中,使用 Path 类处理文件路径。在字符串中设置文件名:string myPath = "D:ew\quiz.txt";现在,要获取扩展名,使用 GetExtension() 方法:Path.GetExtension(myPath)以下是完整代码:示例   在线演示using System; using System.IO; namespace Demo {    class Program {       static void Main(string[] args) {          string myPath = "D:ew\quiz.txt";          Console.WriteLine(Path.GetExtension(myPath));       }    } }输出.txt

C# 程序:获取计算机上的核心总数

Ankith Reddy
更新于 2020-06-22 15:03:52

160 次查看

使用 Environment.ProcessorCount 获取计算机上的核心总数:Environment.ProcessorCount以下代码显示 C# 中计算机上的核心总数:示例Using System; namespace Demo {    class Program {       static void Main(string[] args) {          Console.WriteLine(Environment.ProcessorCount);       }    } }

使用 C# 正则表达式替换字符串部分

Arjun Thakur
更新于 2020-06-22 14:52:47

2K+ 次查看

设置一个字符串:string str = "Bit and Bat";假设你需要将 B 和 t 之间的任何内容替换为 A,并将整个字符串大写。为此,使用 Replace:Regex.Replace(str, "B.t", "BAT");让我们看看完整的代码:示例   在线演示using System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string str = "Bit and Bat";          Console.WriteLine(str);          string res = Regex.Replace(str, "B.t", "BAT");          Console.WriteLine(res);       }    } }输出Bit and Bat BAT and BAT

C# 程序:删除字符串的结尾部分

Chandu yadav
更新于 2020-06-22 14:53:18

473 次查看

在 C# 中,使用 Regex.Replace 方法删除字符串的结尾部分。以下是一个字符串:string s1 = "Demo Text!";现在,假设你需要从字符串中删除感叹号 (!) 。为此,只需使用 replace 将其设置为 empty:System.Text.RegularExpressions.Regex.Replace(s1, "!", "");以下是完整代码:示例   在线演示using System; using System.Text.RegularExpressions; namespace Demo {    class Program {       static void Main(string[] args) {          string s1 = "Demo Text!";          // 替换结尾部分          string s2 = System.Text.RegularExpressions.Regex.Replace(s1, "!", ... 阅读更多

C# 程序:匹配字符串中的所有数字

George John
更新于 2020-06-22 14:53:57

343 次查看

要匹配字符串中的所有数字,请使用 C# 正则表达式。首先,设置一个包含数字的字符串:string str = "These are my marks: 90 out of 100!";使用以下正则表达式在字符串中获取数字:@"\d+"以下是代码:示例   在线演示using System; using System.Text.RegularExpressions; namespace Demo {    class Program {       private static void showMatch(string text, string expr) {          Console.WriteLine("表达式: " + expr);          MatchCollection mc = Regex.Matches(text, expr);          foreach (Match m in mc) {             Console.WriteLine(m);   ... 阅读更多

C# 程序:生成随机小写字母

Ankith Reddy
更新于 2020-06-22 14:54:23

1K+ 次查看

首先,设置 Random 类:Random random = new Random();在 Next() 方法下设置一个范围。这将显示 0 到 26 之间的字母。int a = random.Next(0, 26);以下是完整代码:示例   在线演示using System; using System.IO; using System.Linq; class Demo {    static void Main() {       Random random = new Random();       // 随机小写字母       int a = random.Next(0, 26);       char ch = (char)('a' + a);       Console.WriteLine(ch);    } }输出t

C# 中的字符串随机化

Arjun Thakur
更新于 2020-06-22 14:54:50

1K+ 次查看

要随机化字符串,首先使用 Random 类:Random r = new Random();现在,使用带有 OrderBy() 的 Next() 方法:string random = new string(str.ToCharArray().OrderBy(s => (r.Next(2) % 2) == 0).ToArray());以下是显示随机化字符串的完整代码:示例   在线演示using System; using System.IO; using System.Linq; class Demo {    static void Main() {       const string str = "electronics";       Random r = new Random();       string random = new string(str.ToCharArray().OrderBy(s => (r.Next(2) % 2) == 0).ToArray());       Console.WriteLine("字符串 = {0}", str);       Console.WriteLine("随机字符串 = {0}",random);       Console.Read();    } }输出字符串 = electronics 随机字符串 = lericsecton

C# 程序:显示临时文件名

karthikeya Boyini
更新于 2020-06-22 14:55:40

175 次查看

C# 中的 GetTempPath() 方法显示临时文件名:Path.GetTempPath();将名称保存在变量中并显示:string tempFile = Path.GetTempPath();以下是代码:示例   在线演示using System; using System.IO; class Demo {    static void Main() {       string tempFile = Path.GetTempPath();       Console.WriteLine(tempFile);    } }输出/tmp/

广告
© . All rights reserved.