Asp.net string segmentation function usage, asp.net string Function
Let's look at a simple example.
But its array length is 25, rather than 3. The following method replaces "[111cn.net]" with a special character, for example, $.
For example, if I want to split it according to [111cn.net ],
The Code is as follows:
String [] arrstr2 = str. Replace ("[111cn.net]", "$"). Split ('$ ');
Check other methods.
, The simplest and most commonly used method. Open vs.net to create a console project by separating a specified character. Enter the following program in the Main () method.
The Code is as follows:
String s = "abcdeabcdeabcde ";
String [] sArray = s. Split ('C ');
Foreach (string I in sArray)
Console. WriteLine (I. ToString ());
Output the following results:
AB
Deab
Deab
De
2. Use multiple characters for segmentation
The Code is as follows:
String s = "abcdeabcdeabcde"
String [] sArray1 = s. Split (new char [3] {'C', 'D', 'E '});
Foreach (string I in sArray1)
Console. WriteLine (I. ToString () (www.111cn.net );
Output the following results:
AB
AB
AB
3. Use Regular Expressions
Add reference
The Code is as follows:
Using System. Text. RegularExpressions;
String content = "agcsmallmacsmallgggsmallytx ";
String [] resultString = Regex. Split (content, "small", RegexOptions. IgnoreCase)
Foreach (string I in resultString)
Console. WriteLine (I. ToString ());
Output the following results:
Agc
Mac
Ggg
Ytx
There is also an uncommon Method
The Code is as follows:
The Code is as follows:
String str = "reterry [111cn.net] is the webmaster of [111cn.net ";
String [] arrstr = str. split (new char [] {'[','s ', 'O','s', 'U', 'O', '8 ','. ', 'C', 'O', 'M','] '});
For (int I = 0; I <arrstr. Length; I ++)
{
Response. Write (arrstr [I]);
}
From: http://www.111cn.net/net/net/41570.htm
Aspnet delimiter string
String str = "hg, cr, love, you ";
String [] arrStr = str. Split (',');
String newStr = string. Empty;
Foreach (string item in arrStr)
NewStr + = "'" + item + "',";
If (newStr! = String. Empty)
NewStr = newStr. Substring (0, newStr. Length-1 );
If you have any questions, please HI me ~
Help me use C # To write a function to split strings. Do not use the split function directly. Thank you!
// I Don't Have Visual studio on this computer. I just want to write some pseudocode. You can compile it yourself.
// This code has not been compiled, and the case is basically not separated. Let's take a look.
Public string [] MySplit (string str, char splitChar)
{
// This List class should not be used here. It feels a little heavy, but it cannot be determined that there are several separators in the string
// In fact, it is not complicated to implement a dynamic array by yourself.
// If dynamic arrays are not used, there is an algorithm to declare a character array. The dimension is half the length of a string.
// Finally, check the number of elements used and COPY the elements to a new array. You like it.
List <String> list = new List ();
Int index =-1;
For (int I = 0; I <str. length; I ++)
{
If (string [I] = splitChar)
{
List. add (str. substring (index, I );
Index = I + 1;
}
}
If (index <str. length)
{
List. add (str. substring (index ))
}
Return list. toarray ();
}