0

I have a function in where I want to convert a string value C:\samplec#programs\Converter to C:\\samplec#programs\\Converter Note the differences. This is my function:

private string FilePathProcessor(string path)
{
    char[] oriCharArray = path.ToCharArray; 
    List<char> oriCharList = new List<char>;
    List<char> newCharList = new List<char>;
    foreach (char item in oriCharArray)
    {
        oriCharList.Add(item);
    }

    foreach (char items in oriCharList)
    {
        if ((items != "\\"))
        {
            newCharList.Add(items);
        }
        else
        {
            newCharList.Add(items);
            newCharList.Add(items);
        }
    }

    string result = string.Join(",", newCharList.ToArray());
    return result;
}

Of course this function serves my needs. But, I wonder if there is already an existing function in .Net which will take care of it. I am just cleaning up my code and checking for simpler and faster solution. Not going to reinvent the wheel if there is already a way.

6
  • 1
    Why do you want to do this? Commented Mar 9, 2018 at 2:34
  • Actually, I am placing this value into a mysql database. When I write it in the original form, it misses the "\". I have some hundreds of thousands of these values need to be processed before I could place it into my database table Commented Mar 9, 2018 at 2:36
  • 1
    Have you tried using SomeTypeOfWriteMethod(@"C:\sample#programs\Convertor"); Commented Mar 9, 2018 at 2:37
  • No @programmerj. Didn't tried that Commented Mar 9, 2018 at 2:39
  • Use String.Replace() Commented Mar 9, 2018 at 2:42

1 Answer 1

1

Use String.Replace()

string path = @"C:\samplec#programs\Converter";
string output = path.Replace("\\", @"\\");
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much dude. I feel really stupid for not thinking about this
You do not have to feel stupid, for that we are, to help, luck

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.