February 13th, 2009
Very often you need to change part of a string, maybe just once, or many times over. Strings in .NET/C# are immutable we cannot actually change a string in-place. But we are able to work on copies. The code example below attaches two new methods to the C# string class.
- The ReplaceFirst method replaces the first occurrence of “needle” in a string and replaces it with “replacement”.
- The ReplaceAll function is similar: it steps through the string modifying it each time it finds “needle” and replaces it. To avoid a possible infinite loop it first checks whether “needle” is equivalent to “replacement”.
Read the rest of this entry »
Tags: c#, regex, strings
Posted in Beginner, Learn C# | 2 Comments - getting there! »
February 11th, 2009
A common programming problem is to find the position of all copies of a string in another string. For finding the first copy the C# string method IndexOf is similar to the C strpos() function. It returns the first occurrence of a string in another string. But what if you would like to find the position of all occurances of the substring? The following “IndexOfAll” method does just that. It returns an IEnumerable containing the offsets of each sub-string in the main string.
Read the rest of this entry »
Tags: c#, extension, strings
Posted in Beginner, Learn C# | 2 Comments - getting there! »
February 5th, 2009

Whole programming languages have been designed (*cough* perl) so that we can cut delimited strings into bits and string them back together. For this purpose C# provides the String.Split() and String.Join() functions. You specify how you would like to split or merge the string and they do the work. In this post we look at some common example uses and then put together a simple CSV (comma separated values) parser.
Read the rest of this entry »
Tags: csv, strings
Posted in Beginner, Learn C# | No Comments, yet!
January 6th, 2009
The .NET C# library provides all the basic elements for encrypting a string with a passphrase and decrypting it later. Doing this however requires a few steps in between. This post show a simple set of routines to help you do just that. We use the TripleDES encryption suite to do the actual encryption, with a little help from the MD5 hash sum generator.
The complete source code is listed below, but lets have a little look at how it works first.
Read the rest of this entry »
Tags: decryption, encryption, Learn C#, md5, strings
Posted in Intermediate, Learn C# | 1 Comment already!»