Additional Functions for base classes.

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

I am not sure how to explain what I want but here is an example.

you can use the following

this.tb_MyTextbox.Text.Replace("a","Z")

what I want to do is

this.tb_MyTextBox.Text.MyComplexReplace("adc")

What I want to do is provide code which I can call to work on any string.

Is this possible and if so how can it be done.
 
Jeff,

You can't do this until C# 3.0 comes out (currently in beta). This will
allow you to create extension methods, like so:

public static class StringExtensions
{
public static string MyComplexReplace(this string val)
{
// Do something here.
}
}

And then call it like this:

string s = ...;
s.MyComplexReplace("abc");

However, in your example, it doesn't look like you will be doing
anything, because your replace will have to return a new string (since
strings are immutable, and you don't assign any return value, it can't
affect the original).
 
Thanks Alex

Now i have never used Regex so maybe a quick example of what I need to do

In a string i need to such as

--- This is "a replacement" string and it's replacement need to look
like this ---



--- This is \"a replacement\" string and it\'s replacement need to look
like this ---

How would I achieve this.

Regards
Jeff
 
Looks like you need to replace " by \" and ' by \'

Check this sample for ' and output - same applies for double quote.

using System.Text.RegularExpressions;

....

string t = "This is '";

Console.WriteLine(t);

t = Regex.Replace(t, "'", "\\'");

Console.WriteLine(t);

....

You have to be careful with ' and " - they have special meaning in the
language, so some tricks are necessary
 
Hello AlexS,

Unless you want to replace them all at the same time, using Regex is a very
expensive operation to do single string replacements.

mystring.Replace("what", "replacement")

will get you there.

If you want to replace multiple characters at once, then it gets more exiting.

You can use a regex to specify all of the characters that need to have somthign
prefixed by grouping them into a characterset.

The syntax for a characterset is [characters].

So if you want to replace both ' and " you can put them in a set like this:
["'].

Now for the replacement. The replacement patterns are a little different
than you're used to, because it is possible to include the text you originally
found. This a a very useful feature.

A replacement pattern would look like this:

\$0

\ for the prepended backslash you want to add. And $0 for the string that
we originally found (so in our case either a ' or a ").

Now putting this together yields:

string original = "...";
string replaced = Regex.Replace(original, @"[""'], @"\$0");

and you're all there.

Notice how I used @"..." to make the regex more readable. In a verbatim string
(that's what the @"..." is called) you only need to escape the " by doubling
it up.

This should get you where you want to go...

Kind regards,

Jesse Houwing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top