Core Function NthField
NthField( <string>, <delim>, <index>, <quote>, <quoteescape>, <comment>, <trimtype> )
Contents |
Description
Returns a field from a row of data (such as comma separated text).
This function is useful for reading columns of data from a text file where the columns (fields) are delimited with a specific character or characters and returning a specific one from a given index.
Parameters
string
The string to parse.
delim
The Char to use as the Delimiter for seperation of the data.
index
The column number of the desired field. The first field is numbered 0.
quote
Optional; The Char to use as the Quote this will be allowed to contain the Delimiter inside it.
Default is '"'
quoteescape
Optional; The Char to use for escaping the Quote this will be used to allow a Quote to appear inside quotes.
Default is "\\"
comment
Optional; The Char to use as the Comment and it will be removed from the text with no side effects.
Default is '#'
Can be set to "\0" to disable need for comment
trimtype
Optional; Determines how values should be trimmed.
0 = Trim Unquoted Only (Default)
1 = Trim Quoted Only
2 = Trim All
3 = Trim None
Default is: 0
Return Value
Success: Returns all text from the field at the given location (index).
Failure: Returns empty string.
Remarks
This function uses CSV() for it's parsing needs.
If you wish to return all the fields in arrays instead of just returning a specific field at a specific index then you must use CSV() instead.
Example
The following example prints each Month one by one using the NthField function.
$s = "January,February,March,April,May,June,July,August,September,October,November,December"; $last = CountFields($s,","); For( $i = 0; $i <= $last; $i++) println(NthField($s,",",$i));
This example returns "Smith"
$field = NthField("Dan*Smith*11/22/69*5125554323*Male","*",1); println($field);
This example demonstrates the use of a multiple character separator.
$days = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"; $Wed = NthField($days, ", ", 2); //sets Wed to "Wednesday" println($Wed);