Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Tip/Trick

SuperString Class - Introduction to Making Your Own String Class

Rate me:
Please Sign up or sign in to vote.
2.00/5 (4 votes)
14 Sep 2013CPOL1 min read 15.4K   2   5
A basic shell for creating your own string class since the .NET's string class is sealed leaving it unable to be inherited from

Introduction

This is a basic shell for creating your own string class since the .NET's string class is sealed leaving it unable to be inherited from.

Background

To create your own string class, you will need to setup a few things.

  1. Name your new string type class. I named mine SuperString, this can be anything you want that isn't already defined.
  2. Implicit Operators: string type class must break down to another generic .NET class even if it's the very basic, System.Object. Here's what implicit operators allow for in code: SuperString s = "hello";

The Basic Shell

C#
 public sealed class SuperString
    {
        public string _value;
        public SuperString()
        {
            _value = null;
        }
        public SuperString(string value)
        {
            _value = value;
        }
        //implicit operator: http://stackoverflow.com/questions/3436101/create-custom-string-class
        public static implicit operator SuperString(string value)
        {
            if (value == null)
                return null;
            return new SuperString(value);
        }
        public override string ToString()
        {
            return _value;
        }
}   

Better Version

My version of this class adds in two new properties: CompositionType, CaseType.

  • CompositionType can be one of three values: Mixed, AllNumbers, AllLetters.
  • CaseType can be one of three values: Mixed, Upper, Lower.
C#
using System;
using System.Text;
using System.Text.RegularExpressions;

namespace DataTypes
{
    public class SuperString : System.Collections.Generic.IList<char>
    {
        public enum CompositionType
        {
            Mixed,//default for a standard string
            AllNumbers,
            AllLetters
        }
        public enum CaseType
        {
            Mix,//default for standard string
            Lower,
            Upper
        } 
        private StringBuilder _value;
        public SuperString()
        {
            _value = new StringBuilder(null);
        }
        public SuperString(string value)
        {
            _value = new StringBuilder(value);
        }
        public static implicit operator SuperString(string value)
        {
            if (value == null)
                return null;
            return new SuperString(value);
        }
        public override string ToString()
        {
            return _value.ToString();
        }
        public CompositionType GetCompisition()
        {
            if (IsNumeric(_value))
                return CompositionType.AllNumbers;
            else if (Regex.IsMatch(_value.ToString(), @"^[a-zA-Z]+$"))
                return CompositionType.AllLetters;
            else
                return CompositionType.Mixed;
        }
        public CaseType GetCase()
        {
            if (Regex.IsMatch(_value.ToString(), @"^[A-Z]+$"))
                return CaseType.Upper;
            else if (Regex.IsMatch(_value.ToString(), @"^[a-z]+$"))
                return CaseType.Lower;
            else
                return CaseType.Mix;
        }
        private bool IsNumeric(System.Object Expression)
        {
            if (Expression == null || Expression is DateTime)
                return false;
            if (Expression is Int16 || Expression is Int32 || Expression is Int64 || 
            Expression is Decimal || Expression is Single || 
            Expression is Double || Expression is Boolean)
                return true;
            try
            {
                if (Expression is string)
                    Double.Parse(Expression as string);
                else
                    Double.Parse(Expression.ToString());
                return true;
            }
            catch { } // just dismiss errors but return false
            return false;
        }
    }
}

Points of Interest

After a bit of searching on Google, I found that the string is sealed and complicated to reproduce. I had to set out to create a shell to start with since I will likely keep adding on to this. I also found an open source version of the .NET framework and the base classes like string, int, double, etc.

History

  • Initial post: 9/13/2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United States United States
I started learning HTML in 7th grade (2007) then went on to learn any and everything I could.
HTML/CSS/JavaScript/ActionScript/ASP.Net (6 years)
Basic (5 years)
C/C++ (4 years)
Java (4 years)
C# (3 years)
VB (2 years)

Out of all of these I have to say C# is by far my favorite since its wide spread, works for almost any solution and allows me to use Visual Studio.

Comments and Discussions

 
GeneralMy vote of 2 Pin
FabricePA16-Sep-13 6:48
FabricePA16-Sep-13 6:48 
GeneralMy vote of 2 Pin
ednrg16-Sep-13 5:24
ednrg16-Sep-13 5:24 
GeneralMy vote of 2 Pin
William E. Kempf16-Sep-13 3:44
William E. Kempf16-Sep-13 3:44 
GeneralMy vote of 2 Pin
MenMachine14-Sep-13 8:23
MenMachine14-Sep-13 8:23 
Why not using simply some extension methods?

I cant see any advantages in your class... All you show here is what normally extension methods are made for - expanding any class with new methods.
GeneralMore thoughts Pin
PIEBALDconsult14-Sep-13 7:59
mvePIEBALDconsult14-Sep-13 7:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.