Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend C# built-in types, like String?

Greetings everyone... I need to Trim a String. But I want to remove all the repeated blank spaces within the String itself, not only at the end or at the start of it. I could do it with a method like:

public static string ConvertWhitespacesToSingleSpaces(string value)
{
    value = Regex.Replace(value, @"\s+", " ");
}

Which I got from here. But I want this piece of code to be called within the String.Trim() itself, so I think I need to extend or overload or override the Trim method... Is there a way to do that?

Thanks in advance.

like image 540
Girardi Avatar asked Feb 05 '11 22:02

Girardi


People also ask

Can I extend my C drive to another drive?

In the Disk Management window, right-click on the C drive and select "Shrink Volume". This will open a new window where you can enter the amount of space you want to free up. Next, right-click on the other drives and select "Expand Volume". This will add the freed up space to the other drives.

Why can't I extend my drive C?

You will be unable to extend C drive in Windows 10/11 Disk Management when there is no contiguous unallocated space. It is acceptable to get a proper unallocated space by deleting partition.


3 Answers

Since you cannot extend string.Trim(). You could make an Extension method as described here that trims and reduces whitespace.

namespace CustomExtensions
{
    //Extension methods must be defined in a static class
    public static class StringExtension
    {
        // This is the extension method.
        // The first parameter takes the "this" modifier
        // and specifies the type for which the method is defined.
        public static string TrimAndReduce(this string str)
        {
            return ConvertWhitespacesToSingleSpaces(str).Trim();
        }

        public static string ConvertWhitespacesToSingleSpaces(this string value)
        {
            return Regex.Replace(value, @"\s+", " ");
        }
    }
}

You can use it like so

using CustomExtensions;

string text = "  I'm    wearing the   cheese.  It isn't wearing me!   ";
text = text.TrimAndReduce();

Gives you

text = "I'm wearing the cheese. It isn't wearing me!";
like image 110
JoeyRobichaud Avatar answered Oct 04 '22 17:10

JoeyRobichaud


Is it possible? Yes, but only with an extension method

The class System.String is sealed so you can't use overriding or inheritance.

public static class MyStringExtensions
{
  public static string ConvertWhitespacesToSingleSpaces(this string value)
  {
    return Regex.Replace(value, @"\s+", " ");
  }
}

// usage: 
string s = "test   !";
s = s.ConvertWhitespacesToSingleSpaces();
like image 39
Henk Holterman Avatar answered Oct 01 '22 17:10

Henk Holterman


There's a yes and a no to your question.

Yes, you can extend existing types by using extension methods. Extension methods, naturally, can only access the public interface of the type.

public static string ConvertWhitespacesToSingleSpaces(this string value) {...}

// some time later...
"hello world".ConvertWhitespacesToSingleSpaces()

No, you cannot call this method Trim(). Extension methods do not participate in overloading. I think a compiler should even give you a error message detailing this.

Extension methods are only visible if the namespace containing the type that defines the method is using'ed.

like image 11
Arne Avatar answered Sep 30 '22 17:09

Arne