C#中Split的使用

本文详细介绍了C#中String.Split方法的六种重载形式,包括如何使用不同的分隔符、选项以及限制返回的子字符串数量。通过实例演示了如何处理包含多个分隔符的字符串,并解释了如何避免保留空字符串。

String.Split有六个重载方法

private string _testStr = "(AAA)123(BBB)";
    void Start()
    {
        string[] _temp;
        //public String[] Split(params char[] separator);
        //按参数字符拆分字符串,保留空字符
        _temp = _testStr.Split('(');
        LogSplitStr(_temp);//[],[AAA)123],[BBB)],
        _temp = _testStr.Split('(', ')');
        LogSplitStr(_temp);//[],[AAA],[123],[BBB],[],
        //public String[] Split(char[] separator, StringSplitOptions options);
        //按字符数组拆分字符串,不保留空字符
        _temp = _testStr.Split(new char[] { '(', ')' }, StringSplitOptions.RemoveEmptyEntries);
        LogSplitStr(_temp);//[AAA],[123],[BBB],
        //public String[] Split(String[] separator, StringSplitOptions options);
        //按字符串数组拆分字符串,不保留空字符
        _temp = _testStr.Split(new string[] { "AA", "BB" }, StringSplitOptions.RemoveEmptyEntries);
        LogSplitStr(_temp);//[(],[A)123(],[B)],
        //public String[] Split(char[] separator, int count);
        //按字符数组拆分字符串,返回字符串数组最大长度为Count
        _temp = _testStr.Split(new char[] { '(', ')' }, 5);
        LogSplitStr(_temp);//[],[AAA],[123],[BBB],[],
        //public String[] Split(char[] separator, int count, StringSplitOptions options);
        //按字符数组拆分字符串,返回字符串数组最大长度为Count 不保留空字符
        _temp = _testStr.Split(new char[] { '(', ')' },6, StringSplitOptions.RemoveEmptyEntries);
        LogSplitStr(_temp);//[AAA],[123],[BBB],
        //public String[] Split(String[] separator, int count, StringSplitOptions options);
        //按字符串数组拆分字符串,返回字符串数组最大长度为Count 不保留空字符
        _temp = _testStr.Split(new string[] { "AA", "BB" }, 6,StringSplitOptions.RemoveEmptyEntries);
        LogSplitStr(_temp);//[(],[A)123(],[B)],
    }
    private void LogSplitStr(string[]_temp)
    {
        string _tempStr = "";
        for (int i = 0; i < _temp.Length; i++)
        {
            _tempStr = _tempStr +"["+ _temp[i] + "],";
        }
        Debug.Log(_tempStr);
    }

正常的字符串分割就是传一个参数。

如果要分割的字符串里面有多个需要分割的字符时,可以传入多个分隔符。

特殊情况,如果头尾有分隔符,或者两个分隔符中间没有其他字符时,默认分割之后是保留空字符的,如果不需要空字符的话,可以传入参数StringSplitOptions.RemoveEmptyEntries移除掉空字符。

<think>我们正在回答用户关于C#Split方法的使用教程。根据用户需求,我们将结合引用内容,提供清晰的使用教程。 引用[1]提到:Split()里面必须是一个字符,char类型的数据。但实际上,Split有多个重载,可以接受字符数组、字符串数组等。 引用[2][3]展示了如何自己实现Split函数,但用户更关心的是内置Split方法的使用。 引用[4]给出了一个使用多个分隔符的例子,非常实用。 因此,我们将按以下结构组织回答: 1. Split方法的基本介绍(使用单个分隔符) 2. 使用多个分隔符(字符数组) 3. 使用字符串数组作为分隔符(以及如何避免空条目) 4. 其他常用选项(如StringSplitOptions.RemoveEmptyEntries) 注意:引用[1]说Split里面必须是一个字符,这是不准确的,因为Split有多个重载。我们将纠正这一点。 另外,我们将生成相关问题。</think>### C# 字符串分割 Split 方法使用教程 在 C# 中,`Split()` 是处理字符串分割的核心方法,主要用于将字符串按指定分隔符拆分成子字符串数组。以下是详细使用指南: #### 1. **基础用法(单个分隔符)** ```csharp string text = "apple,banana,orange"; // 按逗号分割 string[] fruits = text.Split(','); // 输出结果 foreach (string fruit in fruits) { Console.WriteLine(fruit); } /* 输出: apple banana orange */ ``` > 📌 分隔符必须是 `char` 类型(单个字符)[^1] --- #### 2. **多分隔符分割** ```csharp string sentence = "Hello! How are you? I'm fine."; // 按感叹号、问号、句号分割 char[] delimiters = { '!', '?', '.' }; string[] phrases = sentence.Split(delimiters); // 输出结果 foreach (string phrase in phrases) { Console.WriteLine(phrase.Trim()); // Trim()移除首尾空格 } /* 输出: Hello How are you I'm fine */ ``` > 💡 通过字符数组指定多个分隔符[^4] --- #### 3. **移除空条目** 使用 `StringSplitOptions.RemoveEmptyEntries` 过滤空字符串: ```csharp string data = "cat,,dog,,bird"; string[] animals = data.Split( new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries ); // 输出结果 Console.WriteLine($"共分割出: {animals.Length} 个元素"); /* 输出: cat dog bird 共分割出: 3 个元素 */ ``` > ⚠️ 未使用该选项时,会返回包含空字符串的数组(长度为5)[^3] --- #### 4. **字符串分隔符(高级)** ```csharp string content = "STARTHeaderENDSTARTBodyENDSTARTFooterEND"; // 按字符串"START"和"END"分割 string[] parts = content.Split( new string[] { "START", "END" }, StringSplitOptions.RemoveEmptyEntries ); // 输出结果 foreach (string part in parts) { Console.WriteLine(part); } /* 输出: Header Body Footer */ ``` --- #### 5. **性能优化技巧** - **预存分隔符数组**:避免重复创建数组 ```csharp private static readonly char[] _delimiters = { ',', ';', '|' }; string[] result = input.Split(_delimiters); ``` - **指定最大分割数**:限制返回数组长度 ```csharp string ip = "192.168.1.1"; string[] segments = ip.Split('.', 2); // 只分割一次 // 结果: ["192", "168.1.1"] ``` --- ### 常见问题解答 **Q: 为什么分割后得到空字符串?** A: 连续分隔符或首尾分隔符会产生空条目,需添加 `StringSplitOptions.RemoveEmptyEntries` 参数[^3]。 **Q: 能否用正则表达式分割?** A: 可以,使用 `Regex.Split()` 方法: ```csharp using System.Text.RegularExpressions; string text = "A1B2C3"; string[] result = Regex.Split(text, @"\d"); // 按数字分割 // 结果: ["A", "B", "C"] ``` **Q: 如何保留分隔符?** A: 需自定义分割逻辑(参考引用[2]的实现原理),内置方法不支持该功能。 > 完整源码实现可参考引用[2][3]中的自定义分割方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值