Thursday, December 16, 2010

Replacing character in string in C#

The "string" library in C# provides many features which makes string manipulation a piece of cake. Of these many functions, one is the function "Replace".

As the name suggests 'Replace', replaces all occurrences of one character, or a string with another provided character of string. (Eg. below would clear the concept)

The function has one overload.

Replace(char oldvalue, char newvalue)
Replace(string oldvalue, string newvalue)

The first one is used to replace occurrence of a particular character, for example, you want to replace all occurrences of 1 with 2.

The second one is used to replace occurrence of a particular string (oldvalue) with a provided string (newvalue).

Example:


string str = "this is a sample string.";
Console.WriteLine(str);
string strReplaced = str.Replace('.', ',');
Console.WriteLine(strReplaced);
strReplaced = str.Replace("this", "given");
Console.WriteLine(strReplaced);


Output:

this is a sample string.
this is a sample string,
given is a sample string.



Example 1


Please note that the comma replaced in the second step is not there anymore in the third step. This is because the replace function did not replace AND store the full stop with comma in the string str, rather it replaced the full stop with the comma and stored it in strReplaced only.

The point to note is that the replace is not done in place, rather the string remains unchanged and a new string is returned by the function with the replaced value.

So if you want the same string to have the replaced values, what you can do is assign the string output returned by the 'Replace' function with the same string.

That is,

string str = "this is a sample string.";
Console.WriteLine(str);
str = str.Replace('.', ',');
Console.WriteLine(str);
str = str.Replace("this", "given");
Console.WriteLine(str);


Output:
this is a sample string.
this is a sample string,
given is a sample string,



Example 2

Monday, December 13, 2010

Auto indenting code in visual studio

Here comes the first informative post. This is something I learnt today. I have been working on dynamic code generation and the code filed generated always seemed to be a bit out on formatting. What I did was selected each line and then either pressed the tab key on my keyboard, or the right indentation button on Visual Studio 2010 IDE.

So today I searched  "auto indentation in vs " on google and the first result I got solved my problem. What it suggests is:

  • Select the code you want/need to indent.
  • Press Ctrl + K + F

Now don't say tab or the right indentation button would have the same effect. Because they won't. With tab/indentation button all the text selected will be pushed by 5 spaces, while Ctrl + K + F would result in proper formatting. Only the lines that need to be pushed will be pushed, the ones which are already at their proper plac would stay where they are supposed to. 

Visual Studio IDE I have tried this trick on:
Visual Studio 2010

Reference:

Launch of IT Abode

Assalam-o-ailikum to all my readers (though I know I don't have any at the present) :D

Everyday we learn something new while developing softwares, removing any glitch from our pcs or just reading articles. And with this blog I plan to collect all this new stuff that I learn at one place. I hope and will try that this place will prove to be helpful to the masses. :)

Have fun reading my blog. :)

Thanks.