SoFunction
Updated on 2025-04-06

C# Split function splits strings based on specific separators

In the development process of C# program, it is often necessary to divide strings into characters or List collections according to specific segmentation characters, such as splitting strings into arrays according to commas, or splitting strings into arrays according to vertical lines. C# provides a Split() function to quickly split strings into arrays. If you need to convert them to List collections, you can use the ToList() method of the array to convert them into List collection data after the segmentation is completed.

For example, the following example separates the character str into an array by commas.

string str = "A,B,C,D,E,F,G";

string[] strArr = (',');//Separate string str according to comma

The resulting array strArr after separation is completed, take the value as

strArr[0]="A",strArr[1]="B",strArr[2]="C",strArr[3]="D",strArr[4]="E",strArr[5]="F"

In some business requirements, it may be necessary to separate the string into a List collection according to specific characters. You can add a ToList to the above. The implementation statement is as follows:

List<string> strList = (',').ToList();

Supplementary knowledge:C# implements the string to split according to multiple characters using the Split method to obtain an array

How to split a String string by multiple characters using the Split method? This article provides the implementation methods of VS2005 and VS2003. VS2005 can use the following methods:

string agentInfo = userInfo.();
      string[] myAgent = (new string[] { "$#$" }, );
      if ( == 3)
      {
         = myAgent[0].ToString();
         = myAgent[1].ToString();
         = myAgent[2].ToString();
      }

Use the following method under VS2003:

1. Separate by string:

using ; 
string str="aaajsbbbjsccc";
string[] sArray=(str,"js",);
foreach (string i in sArray) (() + "<br>");

Output result:

aaa

bbb

ccc

2. Separate with multiple characters:

string str="aaajbbbscccjdddseee";
string[] sArray=(new char[2]{'j','s'});
foreach(string i in sArray) (() + "<br>");

Output result:

aaa

bbb

ccc

ddd

eee

3. Separate with a single character:

string str="aaajbbbjccc";
string[] sArray=('j');
foreach(string i in sArray) (() + "<br>");

Output result:

aaa

bbb

ccc

The above C# Split function is the operation of splitting strings based on specific separators. I hope it can give you a reference and I hope you can support me more.