Namespaces: System.String.Split
Assembly: mscorlib (mscorlib.dll)
Simple example:
String before = "12,50,30";
String[] After =before. Split (New char[]{', '});
The result is after[0] = 12; AFTER[1] = 50; AFTER[2] = 30;
1. Regular expression
If the strings are in mixed mode, that is, they have different types at the same time, you can split their elements using the following methods.
usingSystem;usingSystem.Text.RegularExpressions; Public classexample{ Public Static voidMain () {string[] expressions= {"+ +","* 3","28/3", "42-18","* 7", "2, 4, 6, 8" }; String pattern=@"(\d+) \s+ ([-+*/]) \s+ (\d+)"; foreach(varExpressioninchexpressions)foreach(Match minchregex.matches (expression, pattern)) { intvalue1 = Int32.Parse (m.groups[1]. Value); intvalue2 = Int32.Parse (m.groups[3]. Value); Switch(m.groups[2]. Value) { Case "+": Console.WriteLine ("{0} = {1}", M.value, value1 +value2); Break; Case "-": Console.WriteLine ("{0} = {1}", M.value, value1-value2); Break; Case "*": Console.WriteLine ("{0} = {1}", M.value, value1 *value2); Break; Case "/": Console.WriteLine ("{0} = {1:n2}", M.value, value1/value2); Break; } } }}//The example displays the following output://+ + = Notoginseng//* 3 =//28/3 = 9.33//42-18 =//* 7 =
\s-
Match a whitespace character followed by a hyphen.
\s?
Match zero or one whitespace character.
[+*]?
Match zero or one occurrence of either the + or * character.
\s?
Match zero or one whitespace character.
-\s
Match a hyphen followed by a whitespace character.
usingSystem;usingSystem.Text.RegularExpressions; Public classexample{ Public Static voidMain () {String input="[This is captured\ntext.] \n\n[\n"+"[This was more captured text.] \n]\n"+"[Some more captured text:\n Option1"+"\ Option2] [Terse text.]"; String pattern=@"\[([^\[\]]+)\]"; intCTR =0; foreach(Match minchregex.matches (input, pattern)) Console.WriteLine ("{0}: {1}", ++ctr, m.groups[1]. Value); }}//The example displays the following output://1:this is captured//text.//2:this is more captured text.//3:some more captured text://Option1//Option2//4:terse text.
\[
Match an opening bracket.
([^\[\]]+)
Match any character a opening or a closing bracket one or more times. This is the first capturing group.
\]
Match a closing bracket.
2. Search for specified characters
usingSystem;usingSystem.Collections.Generic; Public classexample{ Public Static voidMain () {String value="This is the first sentence in a string."+"More sentences'll follow. For example,"+"This is the third sentence. the"+"fourth. and this is the fifth and final"+"sentence."; varsentences =NewList<string>(); intPosition =0; intStart =0; //Extract sentences from the string. Do{Position= value. IndexOf ('.', start); if(Position >=0) {sentences. ADD (value. Substring (start, Position-Start +1). Trim ()); Start= Position +1; } } while(Position >0); //Display the sentences. foreach(varSentenceinchsentences) Console.WriteLine (sentence); }}//The example displays the following output://This is the first sentence in a string.//More sentences'll follow.//For example, this is the third sentence.//This is the fourth.//And this is the fifth and final sentence.
IndexOf , returns the position of a particular character or the first occurrence of a string, which returns the zero-based index of the one occurrence of a character or stri Ng in a string instance.
IndexOfAny , returns the position of one or more specific characters or the first occurrence of a string, which returns the zero-based index in the current string instance Of the first occurrence of any character in a character array.
LastIndexOf , returns the position of a particular character or the last occurrence of a string, which returns the zero-based index of the previous occurrence of a character or String in a string instance.
Lastindexofany, which returns a zero-based index in the current string instance of the last occurrence of any cha Racter in a character array.
String. Indexof/string. LastIndexOf
The IndexOf method is used to search for the first occurrence of a particular character or substring in a string, which is case-sensitive and counts from the first character of the string, starting at 0. Returns 1 if the character or substring is not contained in the string.
Positioning characters:
int IndexOf (char value)
int IndexOf (char value, int startIndex)
int IndexOf (char value, int startIndex, int count)
Locating substrings:
int IndexOf (string value)
int IndexOf (string value, int startIndex)
int IndexOf (string value, int startIndex, int count)
In the overloaded form above, the parameters have the following meanings:
Value: The character or substring to be positioned.
StartIndex: The location where the search begins in the total string.
Count: The number of characters to search from the start position in the total string
For example:
String str = "So proud of the young Master's body running the life of the church son so proud of the young Master's body running the life of the church son";
String str1 = str. IndexOf ("Baidu"). ToString (); Returns-1
String str2 = str. IndexOf ("Master"). ToString (); Returns 4
string str3 = str. IndexOf ("Master", 10). ToString (); Returns 19 Description: This is the beginning of the 10th character search.
string STR4 = str. IndexOf ("Ye", 10, 5). ToString (); Returns-1
string STR5 = str. IndexOf ("Ye", 10, 20). ToString (); Returns 20 Description: from the 10th character search, the range to look for is 20 characters from the beginning of the 10th character, that is, from the 第10-30个 character.
Similar to IndexOf, LastIndexOf is used to search for the last occurrence of a particular character or substring in a string, with the same method definition and return value as indexof.
Indexofany/lastindexofany
The IndexOfAny method function is similar to indexof, except that it can search for the first occurrence of any character in a string that appears in a character array. Similarly, the method is case-sensitive and counts from the first character of the string, starting with 0. Returns 1 if the character or substring is not contained in the string. There are 3 common types of IndexOfAny overloads:
int IndexOfAny (char[]anyof);
int IndexOfAny (char[]anyof, int startIndex);
int IndexOfAny (char[]anyof, int startIndex, int count).
In the overloaded form above, the parameters have the following meanings:
AnyOf: An array of characters to be positioned, and the method returns the position of the first occurrence of any character in the array.
StartIndex: The location where the search begins in the original string.
Count: The number of characters to search in the original string starting from the starting position.
For example:
String s = "Hello";
Char[] anyOf = {' H ', ' e ', ' l '};
int i1 = S.indexofany (anyOf); Returns 0
int i2 = S.lastindexofany (anyOf); Returns 3
Similar to IndexOfAny, Lastindexofany is used to search for the last occurrence of any character in a character array in a string.
C # split string