Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

SuperString Class - Introduction to Making Your Own String Class

, 14 Sep 2013
Rate this:
Please Sign up or sign in to vote.
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

 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.
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)

Share

About the Author

shelby67
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 PinmemberFabricePA16-Sep-13 6:48 
GeneralMy vote of 2 Pinmemberednrg16-Sep-13 5:24 
There is no reason to do this. Just create extension methods for the string class. I just don't see any benefit to doing this your way.
GeneralMy vote of 2 PinmemberWilliam E. Kempf16-Sep-13 3:44 
GeneralMy vote of 2 PinprofessionalMenMachine14-Sep-13 8:23 
GeneralMore thoughts PinprofessionalPIEBALDconsult14-Sep-13 7:59 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 14 Sep 2013
Article Copyright 2013 by shelby67
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid