Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

c# split()函数

作者:﹏〃对不起╮他是为你哭了   发布日期:2026-03-07   浏览:42

// 示例代码:使用 C# 的 Split() 方法

using System;

class Program
{
    static void Main()
    {
        // 定义一个字符串,包含多个单词,用空格分隔
        string sentence = "Hello world this is a test";

        // 使用 Split() 方法将字符串按空格分割成字符串数组
        string[] words = sentence.Split(' ');

        // 输出每个单词
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
    }
}

解释说明:

  • Split() 是 C# 中用于将字符串按照指定的分隔符拆分成子字符串的方法。
  • 在上面的例子中,我们定义了一个包含多个单词的字符串 sentence,这些单词之间用空格分隔。
  • 调用 sentence.Split(' ') 方法后,字符串将被分割成一个字符串数组 words,其中每个元素都是原字符串中的一个单词。
  • 最后,我们使用 foreach 循环遍历并输出每个单词。

如果你需要其他类型的分隔符(如逗号、分号等),可以传递相应的字符给 Split() 方法。

上一篇:c#界面开发

下一篇:c#运算符

大家都在看

c# 二进制

c# 创建目录

c# datatable group by

c# tcp client

c# type.gettype

c# sqlconnection

c# string.format 小数位数

.net和c#

c#获取系统时间

c#游戏开发

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站