Processing string values is an indispensable part of most application development projects. This often involves breaking string analysis into separate values. For example, accepting data from external data sources such as workbooks often uses values in some common formats, such as values separated by commas .. . NET String class simplifies the process of extracting values between commas.
Extracted value
The Split method of the String class allows you to extract values separated by specific characters. The delimiter value is passed to the method. The latter uses its second variant to accept that the second parameter is overloaded. this parameter is used to specify the maximum number of returned elements. (Note: You can specify to use more than one Separator in a character array .) The value extracted from the String is returned to the String array.
The following are two variables:
- C # String. Split (char []) in the language
- C # String. Split (char [], int) in the language)
String values = "TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com ";
String [] sites = values. Split (',');
Foreach (string s in sites ){
Console. WriteLine (s );
}
The generated result is as follows:
TechRepublic.com
CNET.com
News.com
Builder.com
GameSpot.com
You can also specify multiple separators and place them in a character array. The following code uses commas, colons, or semicolons to separate string values. In addition, it uses the optional second parameter and sets the maximum number of returned values to 4.
Char [] sep = new char [3];
Sep [0] = ',';
Sep [1] = ':';
Sep [2] = ';';
String values = "TechRepublic.com: CNET.com, News.com, Builder.com; GameSpot.com ";
String [] sites = values. Split (sep, 4 );
Foreach (string s in sites ){
Console. WriteLine (s );
}
The following is the result (note that the second parameter places the rest of the string in the final array element ):
TechRepublic.com
CNET.com
News.com
Builder.com; GameSpot.com
Although the Split method makes it easy to use each element in a string value, you may still need to format all values according to the predefined format (such as values separated by commas. The String class makes it easy to synthesize strings in the correct format.
Synthesize string
The String Join method accepts the characters used as separators as the first parameter. The value to be connected is passed as the second parameter in the form of a string array. It has an overloaded method signature, which can accept the integer as the third and fourth parameters. The third parameter is used to specify the first array element to be used, and the last parameter is the total number of elements to be used.
The following C # code example illustrates the process of merging string values in the previous example:
String sep = ",";
String [] values = new String [5];
Values [0] = "TechRepublic.com ";
Values [1] = "CNET.com ";
Values [2] = "News.com ";
Values [3] = "Builder.com ";
Values [4] = "GameSpot.com ";
String sites = String. Join (sep, values );
Console. Write (sites );
The generated result is as follows:
TechRepublic.com, CNET.com, News.com, Builder.com, GameSpot.com
The START Number of the element and the maximum number returned must be within the range of the string array used. If neither of the two is correct (that is, beyond the range of the array), an exception occurs. Therefore, it is a good idea to use try/catch to solve the problem.
Although the String class provides the necessary methods, it is not the only method used to analyze String values. Another common method is to use regular expressions.
From http://www.cnblogs.com/Apollo/archive/2006/03/11/347928.html