Core Function CountFields

From Sputnik Wiki
Jump to: navigation, search
CountFields( <string>, <delim>, <quote>, <quoteescape>, <comment>, <trimtype> )

Contents

Description

Returns the number of values (fields) in the string passed that are separated by the separator string passed.

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 how many there are.

Parameters

string

The string to parse.

delim

Optional; The Char to use as the Delimiter for seperation of the data.

Default is ','

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 number of how many values (fields) there are.

Failure: Returns 0.

Remarks

This function uses CSV() for it's parsing needs.

If you wish to return the fields in arrays instead of counting how many there are then you must use CSV() instead.

Example

// Prints 3 since there is 3 values in this string seperated by "," 
println("CountFields: " . CountFields("One,Two,Three", ","));

The example below returns 5.

$s = "Dan*Smith*11/22/69*5125554323*Male";
$count = CountFields($s, "*");
println($count);

The following example returns three because it counts the null "field" after the (unnecessary) final field delimiter.

$s = "Dan*Smith*";
$count = CountFields($s, "*");
println($count);

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));
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox