0

I have strings like this:

/Administration/References
/Administration/Menus/Home

etc

Is there an easy way that I can find the 1st, 2nd and 3rd words that appear in these strings and place it into an array. ie. the text between the slashes?

1
  • 3
    String.Split is what you want. Search for that either here on the MSDN for more information. Commented Feb 20, 2012 at 8:56

4 Answers 4

5

The easiest way in this case is

var words = myString.Split(new[]{'/'}, StringSplitOptions.RemoveEmptyEntries);

This will give you an array of all the words seperated by the slashes.

The StringSplitOptions.RemoveEmptyEntries will make sure that you don't get empty entries, since the string is starting with a / it will give an empty first element in the array. If you have a trailing / it will give a empty last element as well.

Sign up to request clarification or add additional context in comments.

Comments

1
string.Split(new char[] { '/' })

See MSDN for more info: http://msdn.microsoft.com/en-us/library/b873y76a.aspx

Comments

1

I think what you are looking for is the split method on string i.e.

string[] words = yourstring.Split('/');

Comments

0

It will give you a List that contains 1st line, 2nd line and etc. Each list item is an Array of strings that you want to parse.

    private List<string[]> ParseText(string text)
    {
        string[] lines = text.Split(new string[] { System.Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        List<string[]> list = new List<string[]>();
        foreach (var item in lines)
        {
            list.Add(item.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries));
        }
        return list;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.