This example summarizes the split usage in C #. Share to everyone for your reference, specific as follows:
Here are two different people that I have reproduced, which are convenient for everyone and their own reference.
String s= "ABCDEABCDEABCDE";
String[] Sarray=s.split ("C");
foreach (String i in Sarray)
Console.WriteLine (i.ToString ());
Output results:
Ab
Deab
Deab
De
String s= "Abcdeabcdeabcde
string[] sarray1=s.split (new char[3]{" C "," D "," E "});
foreach (String i in SArray1)
Console.WriteLine (i.ToString ());
Output results:
Ab
Ab
Main ():
System.Text.RegularExpressions
string content= "Agcsmallmacsmallgggsmallytx";
String[]resultstring=regex.split (Content, "small", Regexoptions.ignorecase)
foreach (String i in resultstring)
Console.WriteLine (i.ToString ());
Output results:
Agc
Mac
Ggg
Ytx
What's the benefit of using regular expressions? Don't worry, we'll see its uniqueness in the back.
The 4th method is described below. Like what
String str1= "I am * * * * * * * * * * * * * * * * * * * * *;
If I want to show as: I am a teacher, how to do it? We can use the following code:
String str1= "I am * * * * * * * * * * * * * * * * TEACHERS;
String[] str2;
Str1=str1. Replace ("* *", "*");
Str2=str1. Split ("*");
foreach (String i in str2)
Console.WriteLine (i.ToString ());
This can also get the correct results. But like
String Str1= "I * * * * * * * * * * is a * * * * * * * * * *;
The result I want to display is: I am a teacher.
If I use the fourth method above, I will create the following error: I am a teacher
There is a space output in the middle, so the output is not the result I hope, how to solve it? This goes back to the following fifth method:
String Str1= "I * * * * * * * * * * is a * * * * * * * * * *;
string[] str2 = System.Text.RegularExpressions.Regex.Split (str1,@ "[*]+");
foreach (String i in str2)
Console.WriteLine (i.ToString ());
Here through the "[*]+" cleverly completed our goal.
88888888***********************************
*****************************************************************
Mystr= "1,2,3,4,5"
mystr=split (MyStr, ",") for
i=0 to UBound (mystr)
Response.Write mystr (i)
next
Split function Language Reference
Describe
Returns a one-dimensional array based on 0 that contains the specified number of substrings.
Grammar
Split (expression[, delimiter[, count[, start]])
The syntax of the Split function has the following parameters:
Parameter description
Expression must be selected. A string expression that contains substrings and delimiters. If expression is a zero-length string, Split returns an empty array, which is an array that contains no elements and data.
Delimiter optional. The character used to identify the bounds of the substring. If omitted, use a space ("") as the separator. If delimiter is a zero-length string, returns the set of cell numbers that contain the entire expression string.
Count is optional. The number of substrings returned,-1 indicates that all substrings are returned.
Compare optional. Indicates the numeric value of the comparison type to use when calculating the substring. For numeric values, see the "Settings" section.
Set up
The Compare parameter can have the following values:
Constant numerical description
Vbbinarycompare 0 performs binary comparisons.
vbTextCompare 1 performs a text comparison.
Vbdatabasecompare 2 performs a comparison based on the information contained in the database (performing comparisons in this database).
Response.Write (Split ("1,2", ","));
Out:
12
Dim MyString, myarray, Msg
MyString = "vbscriptxisxfun!"
MyArray = Split (MyString, "X",-1, 1)
' myarray (0) contains "VBScript".
' MyArray (1) contains ' is '.
' MyArray (2) contains "fun!".
MSG = myarray (0) & "" & MyArray (1)
msg = msg & "" & MyArray (2)
MsgBox msg
With the wrong, sorry:
Str=split ("1,2", ",")
Response.Write Str (1) &str (2)
Out:
12
The STR (2) above is 0.
Read more about C # Interested readers can view the site topics: "C # string Operation Tips Summary", "C # Operation Excel Skills Summary", "C # XML file Operation Tips Summary", "C # Common control usage Tutorial", "WinForm Control Usage Summary", "C # tutorial on data structure and algorithms, Introduction to C # object-oriented programming, and a summary of thread usage tips for C # programming
I hope this article will help you with your C # programming.