SoFunction
Updated on 2025-03-07

Various ways to split strings in C#

In C#, there are several ways to split strings (splitting). The following are common segmentation methods, as well as their performance and applicable scenarios:

1. Use

string text = "apple,banana,orange"; 
string[] result = (','); 

Features

  • Built-in method, easy to use.
  • Supports delimiter arrays, supportStringSplitOptionsto remove empty entries or keep empty strings.

performance

  • High performance, especially in the case of simple separators.
  • Performance decreases slightly when there are multiple delimiters in the delimiter array.

Applicable scenarios

  • Usually used for small-scale string segmentation.
  • Common scenarios such as CSV line parsing or basic string segmentation.

2. Use regular expressions ()

using ; 
string text = "apple123banana456orange"; 
string[] result = (text, @"\d+"); 

Features

  • Supports complex separator patterns (such as regular expressions).
  • Can match dynamic or complex separators (such as numbers, specific character patterns, etc.).

performance

  • Performance ratioLow, especially in the case of complex regular expressions.
  • Compiling and executing regular expressions may introduce additional overhead.

Applicable scenarios

  • The separator is more complex or dynamic.
  • You need to split the string according to the pattern (such as splitting based on numbers or specific character combinations).

3. Use Substring and IndexOf

string text = "apple,banana,orange"; 
int index = (','); 
string firstPart = (0, index); 
string secondPart = (index + 1); 

Features

  • Manually control the segmentation logic, not suitable for complex segmentation.
  • The array will not be returned, it can only be divided once.

performance

  • High performance, suitable for splitting strings in a single position.
  • No additional parsing overhead.

Applicable scenarios

  • Some parts of the string need to be extracted at once.
  • The splitting logic is simple (such as the delimiter position is known, or it only needs to be split once).

4. Use Span<T> (C# 7.2+)

ReadOnlySpan<char> text = "apple,banana,orange".AsSpan(); 
int index = (','); 
var firstPart = (0, index); 
var secondPart = (index + 1); 

Features

  • New string objects will not be allocated, and memory will be directly manipulated.
  • Efficient and suitable for high-performance scenarios.

performance

  • Best performance, avoiding additional memory allocation.
  • Suitable for processing string segmentation results that only require short-term survival.

Applicable scenarios

  • High-performance scenarios to avoid GC pressure.
  • Suitable for handling large strings or real-time systems.

5. Use Linq’s GroupBy or Where

using ; 
string text = "apple,banana,,orange"; 
var result = (',').Where(s => !(s)).ToArray(); 

Features

  • The segmentation results are further processed with the help of LINQ's operation chain.
  • Flexible, but the code may be longer.

performance

  • Low performance because additional LINQ operations are involved.
  • Not suitable for performance-sensitive scenarios.

Applicable scenarios

  • The split string results need to be further filtered, filtered or grouped.

6. Use custom segmentation logic

string text = "apple,banana,orange"; 
List<string> result = new List<string>(); 
int start = 0; 
for (int i = 0; i < ; i++) 
    { 
       if (text[i] == ',') 
          { 
             ((start, i - start)); 
             start = i + 1; 
          } 
   } 
((start)); // Add the last segment 

Features

  • Suitable for scenarios where custom segmentation behavior is required.
  • Manually implement segmentation logic, suitable for handling special separators or conditions.

performance

  • If implemented well, the performance is withClose or even better.
  • Manually control memory and logic, with high complexity.

Applicable scenarios

  • The situation where the segmentation logic cannot be completed through existing methods.
  • Specific business requirements or performance optimization scenarios.

Performance comparison

From high to low:Span<T> > (Simple separator) >Substring + IndexOf >  > Linq

Summary of recommended usage scenarios

  • Simple segmentation, for example, splitting a string with a fixed separator.
  • Complex mode segmentation, for example, splitting a string containing a dynamic pattern.
  • High performance requirementsSpan<T>, avoid memory allocation.
  • Specific logical segmentation: Customize segmentation logic to adapt to special scenarios.
  • Further processing results are needed: CombinedLinqor other methods.

This is the end of this article about various ways of string segmentation in C#. For more related C# string segmentation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!