{"title":"C#字符串分割连续空格:高效代码示例与解析","description":"本文介绍了如何使用C#正则表达式来分割字符串中的连续空格,并提供了示例代码和详细的解析。","keywords":"C# 字符串 分割 连续空格 正则表达式 Regex.Split()","content":"在C#中,可以使用正则表达式来分割连续的空格。可以使用Regex.Split()方法来实现这个功能。以下是一个示例代码:\n\ncsharp\nusing System;\nusing System.Text.RegularExpressions;\n\nclass Program\n{\n static void Main(string[] args)\n {\n string input = "Hello World";\n string[] words = Regex.Split(input, @"\s+");\n\n foreach (string word in words)\n {\n Console.WriteLine(word);\n }\n }\n}\n\n\n在上面的示例中,使用Regex.Split()方法将字符串Hello World按照连续的空格分割成多个单词,并将结果存储在字符串数组words中。然后,使用foreach循环遍历数组并打印每个单词。\n\n运行上述代码,输出结果如下:\n\n\nHello\nWorld\n\n\n可以看到,连续的空格被成功分割,并且只返回了非空的单词。"}

C#字符串分割连续空格:高效代码示例与解析

原文地址: https://www.cveoy.top/t/topic/psMG 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录