Show / Hide Table of Contents

Class csvTable

Parses and composes CSV text. CSV table data in memory as a List of string arrays.

public class csvTable
Remarks

CSV is a text format used to store a single table of data in human-readable/editable way. It is a list of lines (called rows or records) containing one or more values (called fields or cells) separated by a separator character.

There is no strictly defined CSV standard. csvTable uses these rules:

  • Fields containing separator characters (default ','), quote characters (default '"') and multiple lines are enclosed in quote characters. Example: "ab, cd".
  • Each quote character in such fields is escaped (replaced) with two quote characters. Example: "ab ""cd"" ef".
  • If a field value starts or ends with ASCII space or tab characters, it is enclosed in quote characters. Example: " ab ". Or use parameter trimSpacesfalse when parsing.
  • Rows in CSV text can have different field count. All rows in in-memory CSV table have equal field count.
Examples
var c = new csvTable();
c.AddRow("A", "B");
c.AddRow("C", "D");
var csv = c.ToString();
print.it(csv);

var k = csvTable.parse(csv);
for (int row = 0; row < k.RowCount; row++) {
	print.it(k[row, 0], k[row, 1]);
}

Namespace: Au
Assembly: Au.dll
Inheritance
object
csvTable

Constructors

Name Description
csvTable()

Initializes new csvTable variable that can be used to add rows. To create new variables from CSV text, file or dictionary, instead use static functions, for example csvTable.parse.

Properties

Name Description
ColumnCount

Gets or sets column count. The get function returns the length of all string arrays in the internal List. The set function can add new columns or remove columns at the right.

this[Index]

Gets or sets fields in a row.

this[Index, int]

Gets or sets a field.

this[int]

Gets or sets fields in a row.

this[int, int]

Gets or sets a field.

Quote

Sets or gets the quote character used when composing CSV text. Initially it is '"'.

RowCount

Gets or sets row count. The get function returns the Count property of the internal List of string arrays. The set function can add new rows or remove rows at the end.

Rows

Gets the internal List containing rows as string arrays.

Separator

Sets or gets the field separator character used when composing CSV text. Initially it is ','.

Methods

Name Description
AddRow(params string[])

Adds new row and sets its fields.

Get(Index, int, out bool)

Gets a field value like "true" or "false" converted to bool. Case-insensitive.

Get(Index, int, out double)

Gets a field value converted to double. See ExtString.ToNumber.

Get(Index, int, out int)

Gets a field value converted to int. See ExtString.ToInt.

Get(Index, int, out long)

Gets a field value converted to long. See ExtString.ToInt.

Get(Index, int, out float)

Gets a field value converted to float. See ExtString.ToNumber.

Get(Index, int, out uint)

Gets a field value converted to uint. See ExtString.ToInt.

Get(Index, int, out ulong)

Gets a field value converted to ulong. See ExtString.ToInt.

InsertRow(int)

Inserts new empty row.

InsertRow(int, params string[])

Inserts new row and sets its fields.

RemoveRow(int, int)

Removes one or more rows.

Save(string, bool)

Composes CSV and saves to a file.

Set(Index, int, bool)

Converts a bool to string "true" or "false" and sets a field.

Set(Index, int, double)

Converts a number to string and sets a field.

Set(Index, int, int)

Converts a number to string and sets a field.

Set(Index, int, long)

Converts a number to string and sets a field.

Set(Index, int, float)

Converts a number to string and sets a field.

Set(Index, int, uint)

Converts a number to hex string and sets a field.

Set(Index, int, ulong)

Converts a number to hex string and sets a field.

ToDictionary(bool, bool)

Creates dictionary from this 2-column CSV table.

ToDictionary<T>(bool, bool, Func<string[], T>)

Creates dictionary from this CSV table of any column count, using a callback function to convert cell strings to dictionary values of any type.

ToString()

Composes CSV text from the internal List of string arrays.

fromDictionary(Dictionary<string, string>)

Creates 2-column CSV table from dictionary keys and values of type string.

fromDictionary<T>(Dictionary<string, T>, Func<T, string>)

Creates 2-column CSV table from dictionary keys and values of any type, using a callback function to convert values to string.

fromDictionary<T>(Dictionary<string, T>, int, Action<T, string[]>)

Creates CSV table of any column count from dictionary keys and values of any type, using a callback function to convert values to cell strings.

load(string, char, char, bool)

Loads and parses a CSV file.

parse(string, char, char, bool)

Parses CSV string and creates new csvTable variable that contains data in internal List of string arrays.