tailieunhanh - C# 3.0 Cookbook phần 2

Sử dụng phương pháp dụ Chia trên lớp String, bạn có thể đặt các thông tin được giới hạn vào một mảng trong ít nhất là một dòng có một chuỗi, có thể từ một tập tin văn bản, mà là giới hạn bởi một hoặc nhiều ký tự. Bạn cần phải lấy mỗi phần của thông tin được giới hạn là một cách dễ dàng nhất có thể. | string delimitedInfoTotal delimitedInfoBegin delimitedInfoEnd string delimitedInfoFinal delimitedInfoTotal delimitedInfoFinal produces the following delimited string 11 12 Checking Savings See Also The Method topic in the MSDN documentation. Extracting Items from a Delimited String Problem You have a string possibly from a text file which is delimited by one or more characters. You need to retrieve each piece of delimited information as easily as possible. Solution Using the Split instance method on the String class you can place the delimited information into an array in as little as a single line of code. For example string delimitedInfo 100 200 400 3 67 string discreteInfo new char foreach string Data in discreteInfo Data The string array discreteInfo holds the following values 100 200 400 3 67 Discussion The Split method returns a string array with each element containing one discrete piece of the delimited text split on the delimiting character s . In the solution the string delimitedInfo is comma-delimited. However it aan be delimited by any type of character or even by more than one character. When there is more than one type of delimiter use code like the following string discreteInfo new char Thisline splitsthe delimitedInfo string whenever one of the three delimiting characters comma colon or space character is found. Extracting Items from a Delimited String 63 The Split method is case-sensitive. To split a string on the letter a in a caseinsensitive manner use code like the following string discreteinfo new char a A Now anytime the letter a is encountered no matter what its erase the Split method views that character as a delimiter. See Also The Method topic in the MSDN documentation. Iterating over Each Character in a String Problem You need to iterate over each character in a string efficiently in order to .

TỪ KHÓA LIÊN QUAN