Asp tutorial. net foreach traversing Arrays
This tutorial provides two examples to illustrate the foreach exploitation. The instance obtains all files from the specified directory and displays them through foreach traversal. */
String pathstr = tbcontentaddress. text. tostring (). trim ();
String path = server. mappath (pathstr );
Int index = path. lastindexof ('');
String filepath = path. substring (0, index );
// Response. write (filepath );
String [] files = directory. getfiles (filepath ,"*.*");
If (files. length> 0)
{
String [] filenamearr;
String filename1 = "";
String filename = "";
Foreach (string filesname in files)
{
Filename = filesname. replace (@ "d: www. bKjia. c0m ","");
Filename = filename. replace (@"","/");
Filenamearr = filename. split ('/');
Filename1 = filenamearr [filenamearr. length-1]. tostring ();
}
}
// Method 2
Arraylist list = new arraylist ();
Foreach (string filesname in files)
{
Stringbuilder str = new stringbuilder ()
Str. append (filesname. replace (@ "d: www.zhutiai.com ",""));
Str. append ("| ");
Str. append (filename. replace (@"","/"));
Str. append ("| ");
Str. append (filenamearr = filename. split ('/'));
Str. append ("| ");
Str. append (filenamearr [filenamearr. length-1]. tostring ());
List. add (str. tostring ());
}
/*
Original implementation
String temp = name. text; // read data from textbox
String str = "";
Foreach (lcourse cou in data. mycourse) // traverse in list <lcourse>
{
If (condition) // judge {
Str = cou. nage
Break; // skip Loop
}
}
Str =