-2

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.

HarryDoe
  • 1
  • 1
  • 1
    [String.Split()](https://msdn.microsoft.com/en-us/library/b873y76a(v=vs.110).aspx) with '/' as a separator will give you an array of 4 strings. Those you can concatenate in any order. – Arthur Kazykhanov Oct 10 '17 at 21:38
  • 1
    Why do you sometimes want a slash character at the start of your string, sometimes not, and sometimes want slash characters replacing backslashes? Note that backslashes are string metacharacters and `"C:\Program Files (x86)\Mozilla Firefox\dictionaries"` is not a C# string. – Dour High Arch Oct 10 '17 at 21:49
  • I edited the main post, there was supposed to be a slash in front of each reproduced string – HarryDoe Oct 11 '17 at 13:30

2 Answers2

0

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;
    }
Valerii
  • 2,147
  • 2
  • 13
  • 27
0

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);
            }
        }
    }
}
Stephen Straton
  • 709
  • 5
  • 12