Forgive me if this is already covered somewhere which I am sure it is but............

All I am trying to do is split a string that contains the \ char ie filename.

Seen a few ideas and after much frustration I have settled with below code. Replace works fine. Split seems to do nothing. If I add my array to my watches then it tells me it doesn't exist in this context as if its completely unavailable. Anyone got some ideas on what could be causing this?

string fName = fPath.Replace("\\", ",");
            string[] fNamearray = null;
            char[] splitchar = { ',' };
            fNamearray = fName.Split(splitchar);

fName after replace = "C:,Documents and Settings,Administrator.GLIDESOLUTIONS,My Documents,ErrorCheckDateFormat.retry.xls"
fNamearray is non existant

Have also tried

string fName = fPath.Replace("\\", ",");
            char[] splitchar = { ',' };
            string[] fNamearray = fName.Split(splitchar);

Thanks in advance for any help

Mark

The problem is with your string array declaration.
string[] fNamearray = null;
According to this definition DataType[] VariableName = new DataType[Number];this should be something like: string[] fNamearray = new string[10];
See also : http://www.functionx.com/csharp/Lesson21.htm

But I cant guarantee that the array is going to fit ie if the full pathname is more than 10 levels deep for example.

I have seen other code around where the array is initialised on the same line as the split. That would be my prefered option so the split tells the array how big to be.

I did try declaring the array as you mentioned but even that still failed. Something odd is going on

MSDN defines the overload of split I am trying to use as below code but even that failed too..

using System;

public class SplitTest {
    public static void Main() {

        string words = "This is a list of words, with: a bit of punctuation" +
                       "\tand a tab character.";

        string [] split = words.Split(new Char [] {' ', ',', '.', ':', '\t' });

        foreach (string s in split) {

            if (s.Trim() != "")
                Console.WriteLine(s);
        }
    }
}

// The example displays the following output to the console:
// This
// is
// a
// list
// of
// words
// with
// a
// bit
// of
// punctuation
// and
// a
// tab
// character

The problem is with your string array declaration.
string[] fNamearray = null;
According to this definition DataType[] VariableName = new DataType[Number];this should be something like: string[] fNamearray = new string[10];
See also : http://www.functionx.com/csharp/Lesson21.htm

How odd

I got it working by adding one line

string fName = fPath.Replace("\\", ",");
            string[] fNamearray;
            char[] splitchar = { ',' };
            fNamearray = fName.Split(splitchar);
            var test = fNamearray[1];

I have found that as soon as I make reference to part of the array then all of a sudden I can see the array in the watcher. But until then its not valid or something. I had not even tried to look at the array because I did not think that it existed.

Anyways I can carry on now.

Thanks for replying regardless.

My 10 was just an example.
Perhaps try this:

string[] fNamearray = fName.Split(new char[] {' , '});

You dont need to use the string.Repalce method. you can split using the '\' character, just double it up to escape it:

string path = @"C:\test\name.txt";
            char[] splitchar = {'\\'};
            string[] nameParts = path.Split(splitchar);

You dont need to use the string.Repalce method. you can split using the '\' character, just double it up to escape it:

string path = @"C:\test\name.txt";
            char[] splitchar = {'\\'};
            string[] nameParts = path.Split(splitchar);

Yes your right. I did the replace to 'dumb' my code down to try and eliminate the issue in case it was the way I was splitting using the backslashes.

As my reply further up says, I managed to get it working by adding a line that referenced the array created by the split. until then I could not even tell if it had created the array and thats what I wasted about 2 hours or more on tonight :)

Oh well :)

I'm not 100% certain as i haven't seen documentation confirming it yet, but i believe that if a variable is declared and assigned to but never used the compiler disposes of it. I guess its a streamlining measure, rather than holding the value in memory if tis never going to be called upon.
I have had the exact same issue as you, i was trying to check the value of a variable but it said it didn't exist. As soon as i 'used' the variable it showed up in the watch.

I'm not 100% certain as i haven't seen documentation confirming it yet, but i believe that if a variable is declared and assigned to but never used the compiler disposes of it. I guess its a streamlining measure, rather than holding the value in memory if tis never going to be called upon.
I have had the exact same issue as you, i was trying to check the value of a variable but it said it didn't exist. As soon as i 'used' the variable it showed up in the watch.

Yeah that makes sense. Strange though that VS did not give me the old warning about a declared variable not being used.

It had been declared and populated but not actually then used somewhere else so it ditched it.

We live n learn :)

TY all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.