HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Implementing Substring in C#

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
substringimplementingstackoverflow

Problem

I was trying just as a practice to implement substring on my own.

the Test is inside the Ctor just because it is easy.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication2
{
    public class Substring
    {
        public Substring()
        {
            string test = "giladdarmonwhatareyoudoing";
            string res = ApplySubString(5, test);
            string res2 = ApplySubString2(5, test);
        }

        public string ApplySubString(int i, string test)
        {
            char[] charArray = test.ToArray();
            StringBuilder sb = new StringBuilder();

            for (; i < charArray.Length; i++)
            {
                sb.Append(charArray[i]);    
            }
            return sb.ToString();       
        }

        public string ApplySubString2(int i, string test)
        {
            char[] charArray = new char[test.Length - i];
            Array.Copy(test.ToCharArray(), i, charArray, 0, test.Length - i);
            return new String(charArray);
        }
    }
}


Please Let me know if you think I'm missing something or can offer a faster way.

Solution

Assorted things pop into my head here.

-
What's wrong with using the String.Substring(Int32) method?

-
At first glance, I would tend to think your 2nd method would be faster, but they really aren't the same. If input i is greater than test.Length, the 2nd method will throw an exception but the first method would return an empty string.

-
I'd suggest having the test string be the first parameter since it's really the object that you are processing upon. Plus you could then make them static extension methods to the input string.

EDIT - Adding extension method:

namespace System
{
    public static class GiladStrExt
    {
        public static string ApplySubString(this string value, int startIndex)
        {
            // You can invoke an extension method as a straight-up call,
            // e.g. GiladStrExt.ApplySubString(test, 5) and the value
            // for test could be null.
            if (value == null) throw new ArgumentNullException("value");
            if ((value.Length == 0) && (startIndex == 0)) return string.Empty;
            // Usually one uses the extension method with non-null values
            // so all we need to worry about is startIndex compared to value.Length.
            if ((startIndex = value.Length)) throw new ArgumentOutOfRangeException("startIndex");
            var sb = new StringBuilder();
            for (int i = startIndex; i < value.Length; i++)
            {
                sb.Append(value[i]);
            }
            return sb.ToString();
        }
    }
}

Code Snippets

namespace System
{
    public static class GiladStrExt
    {
        public static string ApplySubString(this string value, int startIndex)
        {
            // You can invoke an extension method as a straight-up call,
            // e.g. GiladStrExt.ApplySubString(test, 5) and the value
            // for test could be null.
            if (value == null) throw new ArgumentNullException("value");
            if ((value.Length == 0) && (startIndex == 0)) return string.Empty;
            // Usually one uses the extension method with non-null values
            // so all we need to worry about is startIndex compared to value.Length.
            if ((startIndex < 0) || (startIndex >= value.Length)) throw new ArgumentOutOfRangeException("startIndex");
            var sb = new StringBuilder();
            for (int i = startIndex; i < value.Length; i++)
            {
                sb.Append(value[i]);
            }
            return sb.ToString();
        }
    }
}

Context

StackExchange Code Review Q#73744, answer score: 9

Revisions (0)

No revisions yet.