Home About

 February 13th, 2009

Manipulating Strings in C# -Replacing part of a string / Replacing all occurences of a sub-string - 3

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 »

 February 11th, 2009

Manipulating Strings in C# – Finding all occurrences of a string within another string - 5

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 »

 February 5th, 2009

Using String.Split and String.Join to build a simple CSV reader and writer in C# - 0

Creating Delimited Strings in C#
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 »

 January 6th, 2009

Encrypting and Decrypting a C# string - 13

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 »


Recent Comments
  • Ales: Hi, Thanks for the code… I must say I did not experience any errors decrypting any of my messages. I even...
  • JC: Thanks very useful and well explained
  • Thomas: This is a public static class written in the C# language that does not save state. You can call into the...
  • Simon: Thank you very much for this post! It helped me essentially to overcome obstacels to work with mono!
  • Graham: This is a good research for keyboard shortcuts! Some shortcuts are also compatible for Windows OS. I have...