using System;
using System.Collections.Generic;
class Program
{
    static void Main()
    {
        // 定义一个字符串,包含多个单词,用空格分隔
        string input = "C# is a powerful programming language";
        // 使用Split方法,以空格作为分隔符分割字符串
        string[] words = input.Split(' ');
        // 输出分割后的结果
        Console.WriteLine("分割后的字符串数组:");
        foreach (string word in words)
        {
            Console.WriteLine(word);
        }
        // 如果需要使用多个分隔符,可以传递一个字符数组给Split方法
        string input2 = "apple, orange; banana";
        char[] delimiters = new char[] { ',', ';', ' ' };
        string[] fruits = input2.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
        // 输出分割后的结果
        Console.WriteLine("\n分割后的水果数组:");
        foreach (string fruit in fruits)
        {
            Console.WriteLine(fruit);
        }
    }
}input,这些单词之间用空格分隔。Split 方法:通过调用 Split 方法,并传入空格字符 ' ' 作为分隔符,将字符串分割成一个字符串数组 words。delimiters 给 Split 方法。同时,使用 StringSplitOptions.RemoveEmptyEntries 参数可以忽略分割后为空的项。这个示例展示了如何使用 C# 的 Split 方法来分割字符串,并处理不同类型的分隔符。
上一篇:c# webrtc
下一篇:c# bartender
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站