using. NET to easily parse string values process string values is an integral part of most application development projects. This often involves splitting the string analysis into separate values. For example, accepting data from an external data source such as a spreadsheet often uses values that appear in some common formats, such as comma-delimited values. NET simplifies the process of extracting individual values between commas. The split method of extracting values string classes allows you to extract individual values separated by a particular character. The value of the delimiter is passed to the method, which is accepted by its second variant to be overloaded with the second parameter, which specifies the maximum number of elements returned. (Note: You can specify that more than one delimiter is used in an array of characters.) The value extracted from the string is returned to the string array. Below are two variables: String.Split (char[] in the C # language, or String.Split (char[], int) in the String.Split (char ()) C # language in vb.net. or String.Split in vb.net (Char (), Integer) The following C # snippet uses a comma-delimited string value to populate an array: String values = "techrepublic.com, CNET.com, News.com, builder.com, gamespot.com "; string[] Sites = values. Split (', '); foreach (string s in sites) {Console.WriteLine (s);} Following is the result of the build: techrepublic.com cnet.com news.com builder.com Gam espot.com corresponding vb.net code is: DIM values as String values = "techrepublic.com, cnet.com, News.com, builder.com, GameSpot." COM "Dim sites as String () = Nothing sites = values. Split (",") Dim s as String for each s in sites Console.WriteLine (s) Next s You can also specify multipleDelimiter, placed in an array of characters. The following code splits the value of the string with a comma, colon, or semicolon. In addition, it uses the optional second parameter, which sets the maximum number of values returned 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 of the build (note that the second argument places the remainder of the string in the final array element): techrepublic.com CNE T.com news.com builder.com; GameSpot.com corresponding vb.net code is: DIM values as String values = "TechRepublic.com:CNET.com, News.com, builder.com;" GameSpot.com "Dim sites as String () = Nothing Dim Sep (3) as Char Sep (0) =", "sep (1) =": "Sep (2) ="; "Sites = values. Split (Sep, 4) Dim s As string for every s in Sites Console.WriteLine (s) Next s Although the split method makes it easy to use the elements of a string value, you may still need to base the predefined format, such as a comma-separated value, for formatting all the values. The string class makes it easy to synthesize strings of the correct format.
Synthetic StringsThe string class's Join method accepts the character used as the delimiter as the first argument. The value to be joined is passed as the second argument as an array of strings. It has an overloaded method signature that can accept an integer value as a third and a fourth parameter. The third parameter is used to specify the first array element to use, and the last parameter is the total number of elements to use. The following C # code example demonstrates the process of synthesizing the string value 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); Here is the result of the build: Techrepublic.com, CNET.com, News.com, builder.com, gamespot.com the corresponding vb.net code is: Dim Sep as String
Sep = ","
Dim VALUES (4) as String
VALUES (0) = "techrepublic.com"
VALUES (1) = "CNET.com"
VALUES (2) = "News.com"
VALUES (3) = "builder.com"
VALUES (4) = "GameSpot.com"
Dim Sites as String
Sites = String.Join (Sep, values)
Console.Write (sites) We can use overloaded format to specify where to start and how many elements to include in the result. The following example starts with a second (note that the element's number starts at 0) and returns a maximum of three elements: Dim Sep as String
Sep = ","
Dim VALUES (4) as String
VALUES (0) = "techrepublic.com"
VALUES (1) = "CNET.com"
VALUES (2) = "News.com"
VALUES (3) = "builder.com"
VALUES (4) = "GameSpot.com"
Dim Sites as String
Sites = String.Join (Sep, values, 2, 3)
The starting number of the Console.Write (sites) element and the maximum number returned must be within the range of the string array being used. If both are incorrect (that is, beyond the range of the array), an exception occurs. Therefore, it is a good idea to use Try/catch to deal with problems encountered. Although the string class provides the necessary method, it is not the only way to parse string values. Another common way of using regular expressions.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.