Programming Tutorial On The Way

free online programming resource on dotnet, csharp, web related topics

It happens that 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 and you want a simple way to remove these characters. Usually you can use the Trim method of string class to eliminate whitespace at the beginning and end of a string. In fact, if you call Trim without any parameters on a string variable, this is exactly what would happen. The Trim method is overloaded to allow you to remove other types of characters from the beginning and end of a string. You can pass in a char[] containing all the characters that you want removed from the beginning and end of a string. Note that if the characters contained in this char[] are located somewhere in the middle of the string, they are not removed. Read the rest of this entry »

Popularity: 2% [?]

If you have a consistent string whose parts, or tokens, are separated by well-defined characters, the Split method in string class can tokenize the string. Tokenizing a string consists of breaking the string down into well-defined, discrete parts, each of which is considered a token. For example: Read the rest of this entry »

Popularity: 2% [?]

The string class provides two methods that allow easy removal and modification of characters in a string: the Remove instance method and the Replace instance method. The Remove method deletes a specified number of characters starting at a given location within a string. This method returns a new string object containing the modified string. Read the rest of this entry »

Popularity: 3% [?]

When you need to examine each character in a string, you need to iterate the string. C# provides two methods for iterating strings, e.g by using foreach and for. The first is by using a foreach loop, as follows: Read the rest of this entry »

Popularity: 3% [?]

Inserting Text into a String

This is another string manipulation article. 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. There are two ways of inserting strings into other strings, unless, of course, you are using the regular expression classes. The first involves using the Insert instance method on the string class. This method is also slower than the others since strings are immutable, and, therefore, a new string object must be created to hold the modified value, the reference to the old string object is then changed to point to the new string object. Note that the Insert method leaves the original string untouched and creates a new string object with the inserted characters. Read the rest of this entry »

Popularity: 3% [?]

If your application consists of many strings that are compared frequently, you can use the intern pool to improve the resource usage. The CLR automatically stores all string literals declared in an application in an area of memory called the intern pool. The intern pool contains a unique instance of each string literal found in your code, which allows for more efficient use of resources by not storing multiple copies of strings that contain the same string literal. Another benefit is speed. When two strings are compared using either the == operator or the Equals instance method of the string class, a test is done to determine whether each string variable reference is the same; if they are not, then each string’s length is checked; if both string’s lengths are equal, each character is compared individually. However, if we could guarantee that the references, instead of the string contents, could be compared, much faster string comparisons can be made. String interning does just that: it guarantees that the references to equivalent string values are the same, eliminating the possibility of attempting the length and character-by-character checks. This yields better performance in situations where the references to two equal strings are different and the length and character-by-character comparisons have to be made. Read the rest of this entry »

Popularity: 3% [?]

Most of the time, for string manipulation operation, the string datatype is used. The string class is immutable; once a string is assigned to a variable of type string, that variable cannot be changed in any way. So changing the contents of a string variable entails the creation of a new string containing the modified string. The reference variable of type string must then be changed to reference this newly created string object. The old string object will eventually be marked for collection by the garbage collector, and, subsequently, its memory will be freed. Because of this intensive behind-the-scene action, code that performs intensive string manipulations using the string class suffers greatly from having to create new string objects for each string modification, and greater pressure is on the garbage collector to remove unused objects from memory more frequently. Read the rest of this entry »

Popularity: 3% [?]

Formatting Data in Strings

The static string.Format method allows you to format strings in a variety of ways, this is very helpful when you need to output the specific format to the display.

The string.Format method allows a wide range of formatting options for string data. The first parameter of this method can be passed a string that may look similar to the following: Read the rest of this entry »

Popularity: 2% [?]

« Previous Entries