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?
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.
string.Split(new char[] { '/' })
See MSDN for more info: http://msdn.microsoft.com/en-us/library/b873y76a.aspx
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;
}
String.Splitis what you want. Search for that either here on the MSDN for more information.