Summary of split Usage Examples in C
- 2021-10-25 07:43:40
- OfStack
This article summarizes the usage of split in C # with examples. Share it for your reference, as follows:
The following is my reprint of two different people, which is convenient for everyone and themselves to consult
string s="abcdeabcdeabcde";
string[] sArray=s.Split("c");
foreach(string i in sArray)
Console.WriteLine(i.ToString());
Output:
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:
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:
agc
mac
ggg
ytx
What are the benefits of using regular expressions? Don't worry, we will see its uniqueness later.
The fourth method is described below. For example
string str1= "I am a ******1******* teacher";
If I want to show: I am a teacher, what should I do? We can use the following code:
string str1=" I ***** Yes *****1***** A ***** Teaching ***** Division ;
string[] str2;
str1=str1.Replace("*****","*");
str2=str1.Split("*");
foreach(string i in str2)
Console.WriteLine(i.ToString());
In this way, we can also get correct results. But for example
string str1= "I am a ******1******* teacher";
I hope to show the result: I am a teacher.
If I use the fourth method above, I will make the following mistakes: I am a teacher
There is a space output in the middle, so the output result is not what I want. How to solve it? This goes back to the fifth method below:
string str1=" I ** Yes *****1***** A ***** Teaching ***** Division ";
string[] str2 = System.Text.RegularExpressions.Regex.Split(str1,@"[*]+");
foreach(string i in str2)
Console.WriteLine(i.ToString());
Here, we have accomplished our goal skillfully through "[*] +".
*************************88888888***********************************
*****************************************************************
mystr="1,2,3,4,5"
mystr=split(mystr,",")
for i=0 to ubound(mystr)
response.write mystr(i)
next
Split functional language reference
Describe
Returns a 0-based 1-dimensional array containing the specified number of substrings.
Grammar
Split(expression[, delimiter[, count[, start]]])
The syntax of the Split function takes the following parameters:
Parameter description
expression required. String expression containing substrings and delimiters. If expression is a zero-length string, Split returns an empty array, that is, an array that contains no elements or data.
delimiter is optional. The character used to identify the bounds of a substring. If omitted, use a space ("") as the separator. If delimiter is a zero-length string, an array of elements containing the entire expression string is returned.
count is optional. The number of substrings returned,-1 indicates that all substrings are returned.
compare is optional. A numeric value indicating the comparison type used when evaluating substrings. For numerical values, see the Settings section.
Settings
The compare parameter can have the following values:
Constant numerical description
vbBinaryCompare 0 performs binary comparisons.
vbTextCompare 1 performs text comparisons.
vbDatabaseCompare 2 performs a comparison based on the information contained in the database where the comparison is performed.
Response.Write ( split("1,2",",") );
Out:
12
Dim MyString, MyArray, Msg
MyString = "VBScriptXisXfun!"
MyArray = Split(MyString, "x", -1, 1)
' MyArray(0) Include "VBScript" .
' MyArray(1) Include "is" .
' MyArray(2) Include "fun!" .
Msg = MyArray(0) & " " & MyArray(1)
Msg = Msg & " " & MyArray(2)
MsgBox Msg
Wrong use, sorry:
str=split("1,2",",")
response.write str(1)&str(2)
Out:
12
Str (2) above is 0
For more readers interested in C # related content, please check the topics on this site: "C # String Operation Skills Summary", "C # Operation Excel Skills Summary", "C # XML File Operation Skills Summary", "C # Common Control Usage Tutorial", "WinForm Control Usage Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Skills Summary"
I hope this article is helpful to everyone's C # programming.