65.9K
CodeProject is changing. Read more.
Home

SuperString Class - Introduction to Making Your Own String Class

starIconstarIconemptyStarIconemptyStarIconemptyStarIcon

2.00/5 (4 votes)

Sep 14, 2013

CPOL

1 min read

viewsIcon

16516

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