Strings¶
Raw Strings¶
Raw strings are produced using the @ symbol.
string dir = @"/home/human1/down";
Tests / Filters¶
Endswith¶
dirName.EndsWith("done")
Collections (Array, ArrayList, List)¶
Types of Collections¶
Array¶
Array is a non-mutable collection of one type of object.
ArrayList¶
ArrayList is an extendable array. Requires using System.Collections;
Array and ArrayList can have members of different types if they are declared as var
?
List¶
List is ?? (possibly multi-type??)
/* An array of strings, at creation assigned the names of the directories in a folder */
string[] dirrawTop = Directory.GetDirectories(dir);
/* An empty extendable array created*/
ArrayList dirTop = new ArrayList();
Functionality¶
Convert¶
Convert an array of var (or other collection) to an array:
var files = Directory.GetFiles(dir).Where(name => name.EndsWith(".mkv")||name.EndsWith(".mp4"));
string[] filesrawTop = files.ToArray<string>();
To convert out of an array, unbundle with a foreach
loop.
Where¶
Requires using System.Linq;
=>
in this instance is a lambda
var files = Directory.GetFiles(dir).Where(name => name.EndsWith(".mkv")||name.EndsWith(".mp4"));
string[] filesrawTop = files.ToArray<string>();
Replacing Elements¶
Replace first occurence of an item
How do I replace an item in a string array?
Replace all occurences of a string from a string array
Replace all occurences of a string from a string array
string [] items = {"one","two","three","one","two","one"};
string[] items2 = items.Select(x => x.Replace("one", "zero")).ToArray();
Directory management¶
Change directory (cd)¶
requires using System.IO;
Directory.SetCurrentDirectory(dir);
Process Management¶
Subprocess (Start external program)¶
Process.Start Method
How to run external program via a C# program?