HarryDoe
HarryDoe

Reputation: 1

I need to be able to able to extract parts of strings to get other strings

I need to be able to be able to break down a string and get each part of the string like so:

Example: "C:\Program Files (x86)\Mozilla Firefox\dictionaries" (I Start with this)

(and i get these four)

"/C:"
"/C:/Program Files (x86)"
"/C:/Program Files (x86)/Mozilla Firefox"
"/C:/Program Files (x86)/Mozilla Firefox/dictionaries"

I am developing in 4Test and you might not be familiar with it but as long as theres no built in functions in the code I can reproduce it here. I am familiar with c#

Edit**** Sorry There was meant to be a slash in front of each newly reproduced string. It is possible to get a solution not involving String.Split as it is not available in language I am using, I just asked for a solution in c# without built-in functions due to the low popularity of the scripting language I am using.

Upvotes: -2

Views: 89

Answers (2)

Stephen Straton
Stephen Straton

Reputation: 739

You could loop over the characters in the string and build up the buffer as you go. Each time you hit the delimiter add the current buffer to the results list. Best seen in code:

using System;
using System.Collections.Generic;

namespace SplitString
{
    class Program
    {
        public static List<string> GetStrings(string input, char delimiter, string prefix)
        {
            // Pre-load buffer with prefix
            string buffer = prefix;
            List<string> result = new List<string>();

            foreach (var c in input)
            {
                if (c == delimiter)
                {
                    // We have hit a delimeter so we have a result
                    result.Add(buffer);
                    buffer += prefix;
                }
                else
                {
                    buffer += c;
                }
            }
            // At end of string need to add last result
            result.Add(buffer);
            return result;
        }

        static void Main(string[] args)
        {
            var strings = GetStrings(@"C:\Program Files(x86)\Mozilla Firefox\dictionaries ", '\\', "/");
            foreach (var s in strings)
            {
                Console.Out.WriteLine(s);
            }
        }
    }
}

Upvotes: 0

Valerii
Valerii

Reputation: 2317

It is not clear how many and what kind of slashes (forward or backward) should be in the result. I think it is not really important.

Solution:

    public List<string> Split(string path)
    {
        var folders = path.Split(new[] {"\\", ":\\" }, StringSplitOptions.None);
        var result = new List<string>();
        for (var i = 0; i <= folders.Length - 1; i++)
        {
            var subPath = string.Empty;
            for (var j = 0; j <= i; j++)
            {
                subPath += folders[j];
                if (j == 0)
                    subPath += ":\\";
                else
                    subPath += "\\";
            }

            result.Add(subPath);
        }

        return result;
    }

Upvotes: 0

Related Questions