Google


   


You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Strings And Characters

SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

C# Cookbook, 2nd Edition



Problem: You have a variable of type char and wish to determine the kind of character it containsa letter, digit, number, punctuation character, control character, separator character, symbol, whitespace, or surrogate character. Similarly, you have ...



Problem: You need to determine whether a character in a char data type is within a range, such as between the numbers 1 and 5 or between the letters A and M. Solution: Use the built-in comparison support for the char data type. The following c...



Problem: You need to compare two characters for equality, but you need the flexibility of performing a case-sensitive or case-insensitive comparison. Solution: Use the Equals instance method on the char structure to compare the two characters:...



Problem: You need a way of searching a string for multiple occurrences of a specific character. Solution: Use IndexOf in a loop to determine how many occurrences of a character exist, as well as to identify their location within the string: ...



Problem: You need to search a string for every occurrence of a specific string. In addition, the case sensitivity, or insensitivity, of the search needs to be controlled. Solution: Using IndexOf or IndexOfAny in a loop, you can determine how m...



Problem: You need a quick method of breaking up a string into a series of discrete tokens or words. Solution: Use the Split instance method of the String class. For example: string equation = "1 + 2 - 4 * 5"; string[] equationTokens = eq...



Problem: You need to compare the contents of two strings for equality. In addition, the case sensitivity of the comparison needs to be controlled. Solution: Use the Compare static method on the String class to compare the two strings. Whether ...



Problem: You need to determine whether a string is at the head or tail of a second string. In addition, the case sensitivity of the search needs to be controlled. Solution: Use the EndsWith or StartsWith instance method on a string object. Com...



Problem: You have some text (either a char or a string value) that needs to be inserted at a specific location inside of a second string. Solution: Using the Insert instance method of the String class, a string or char can easily be inserted i...



Problem: You have some text within a string that needs to be either removed or replaced with a different character or string. Since the replacing operation is somewhat simple, using a regular expression to aid in the replacing operation is not worth...



Problem: You have a byte[] representing some binary information, such as a bitmap. You need to encode this data into a string so that it can be sent over a binary-unfriendly transport such as email. Solution: Using the static method Convert.To...



Problem: You have a String that contains information such as a bitmap encoded as base64. You need to decode this data (which may have been embedded in an email message) from a String into a byte[] so that you can access the original binary. Sol...



Problem: Many methods in the FCL return a byte[] because they are providing a byte stream service, but some applications need to pass strings over these byte stream services. Some of these methods include: System.Diagnostics.EventLogEntry.Data ...



Problem: Many methods in the FCL accept a byte[] consisting of characters instead of a string. Some of these methods include: System.Diagnostics.EventLog.WriteEntry System.IO.BinaryWriter.Write System.IO.FileStream.Write System.IO.FileStre...



Problem: You have a string that represents the equivalent value of a number ("12"), char ("a"), bool ("true"), or a color enumeration ("Red"). You need to convert this string to its equivalent value type. Therefore, the number "12" would be converte...



Problem: You need to format one or more embedded pieces of information inside of a string, such as a number, character, or substring. Solution: The static string.Format method allows you to format strings in a variety of ways. For example: ...



Problem: You have an array of strings to format as delimited text and possibly to store in a text file. Solution: Using the static Join method of the String class, the array of strings can be easily joined in as little as one line of code. For...



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,...



Problem: You want to insure that the data assigned to a StringBuilder object does not exceed a certain number of characters. Solution: Use the overloaded constructor of the StringBuilder class, which accepts a parameter for maximum capacity. T...



Problem: You need to iterate over each character in a string efficiently in order to examine or process each character. Solution: C# provides two methods for iterating strings. The first is the foreach loop, which can be used as follows: s...



Problem: Your application consists of many strings that are compared frequently. You have been tasked with improving performance and making more efficient use of resources. Solution: Use the intern pool to improve resource usage and, in turn, ...



Problem: In an attempt to improve string-handling performance, you have converted your code to use the StringBuilder class. However, this change has not improved performance as much as you had hoped. Solution: The chief advantage of a StringBu...



Problem: You have a string with a specific set of characters, such as spaces, tabs, escaped single/double quotes, any type of punctuation character(s), or some other character(s), at the beginning and/or end of a string. You want a simple way to rem...



Problem: You need a quick and easy way to check if a string is either null or of zero length. Solution: Use the static IsNullOrEmpty method of the String class: bool stringTestResult = String.IsNullOrEmpty(testString); Discussion ...



Problem: You need to append a line, including a line terminator, to the current string. Solution: Use the AppendLine method of the StringBuilder class: StringBuilder sb = new StringBuilder("First line of string"); // Terminate the firs...

read more: Appending a Line


Problem: You need to encode some data; however, you will be receiving it in blocks of a certain size, not all at once. Your encoder needs to be able to append each block of data to the previous one to reconstitute the entire data stream. Soluti...


SAVE
Digg
Shown on del.icio.us del.icio.us
See Whos Talking About This on Technorati Technorati
I've Reddit reddit

You are here: CodeIdol.com > C# > C# Cookbook, 2nd Edition > Strings And Characters


ADBRITE ads links
   
Related tags







Popular Categories
Unix books and guides

AJAX popular information
C# language guides
Windows books and cookbooks

.......








Business Key Top Sites

be number one
rate your site




    С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.

    Русский Polski Francais Deutsch
    support sitemap terms

© CodeIdol Labs, 2007 - 2009