This article summarizes the usage of split in C#. Share it for your reference, as follows:
The following are two different people I reproduced, so that everyone can check it out for themselves
string s="abcdeabcdeabcde"; string[] sArray=("c"); foreach(string i in sArray) (());
Output result:
ab
deab
deab
de
string s="abcdeabcdeabcde string[] sArray1=(new char[3]{"c","d","e"}); foreach(string i in sArray1) (());
Output result:
ab
ab
Main() :
string content="agcsmallmacsmallgggsmallytx"; string[]resultString=(content,"small",) foreach(string i in resultString) (());
Output result:
agc
mac
ggg
ytx
What are the benefits of using regular expressions? Don't worry, we'll see its uniqueness later.
The fourth method is introduced below. for example
string str1="I **** are ********************** teacher";
If I want to display as: I am a teacher, how to do it? We can use the following code:
string str1="I*****yes*****one*****indivual*****teach*****division; string[] str2; str1=("*****","*"); str2=("*"); foreach(string i in str2) (());
This will also result in the correct results. But for example
string str1="I ** are ********************** teacher";
The result I want to show is: I am a teacher.
If I use the fourth method above to do it, the following error will occur: I am a teacher
There is a space in the middle to output, so the output result is not the result I hoped for. How to solve it? This is back to this point, the following fifth method can be used:
string str1="I am a **************************** teacher"; string[] str2 = (str1,@"[*]+"); foreach(string i in str2) (());
Our goal is cleverly accomplished here through "[*]+".
*************************88888888***********************************
*****************************************************************
mystr="1,2,3,4,5" mystr=split(mystr,",") for i=0 to ubound(mystr) mystr(i) next
Split Function Language Reference
describe
Returns a one-dimensional array based on 0, containing the specified number of substrings.
grammar
Split(expression[, delimiter[, count[, start]]])
The syntax of the Split function has the following parameters:
Parameter Description
expression Required. A string expression containing substrings and separators. If expression is a zero-length string, Split returns an empty array, that is, an array that does not contain elements and data.
Delimiter optional. Characters used to identify the limits of substrings. If omitted, use space ("") as the separator. If delimiter is a zero-length string, a single-element array containing the entire expression string is returned.
count is optional. The number of substrings returned, -1 indicates that all substrings are returned.
compare Optional. Indicates the value of the comparison type used when calculating the substring. For values, see the Settings section.
set up
The compare parameter can have the following values:
Constant Value Description
vbBinaryCompare 0 Perform binary comparison.
vbTextCompare 1 Perform text comparison.
vbDatabaseCompare 2 performs a comparison based on the information contained in the database (in which comparison is performed).
( 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
Used incorrectly, sorry:
str=split("1,2",",") str(1)&str(2)
Out:
12
The above Str(2) is 0
For more information about C# related content, please check out the topic of this site:Summary of C# string operation skills》、《Summary of C# operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.