CountFields

From Xojo Documentation
Jump to: navigation, search
Method

Returns the number of values (fields) in the string passed that are separated by the separator string passed. If the source string is binary data or you require case-sensitivity, use CountFieldsB instead.


Syntax

result=CountFields(source, separator)

Part Type Description
result Integer The number of values in source that are separated by separator.
source String The original string.
separator String The character or characters that separate the values in source.

Notes

The CountFields function is useful for reading columns of data from a text file where the columns (fields) are delimited with a specific character or characters.

If the separator is not found within source, CountFields returns 1. If source is null, CountFields returns zero.


NOTE: Using CountFields in a loop to extract fields from a string is inefficient. You should use Split and Ubound for this purpose.


Examples

The example below returns 5.

Dim count as Integer
Dim s as String
s="Dan*Smith*11/22/69*5125554323*Male"
count=CountFields(s, "*")


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

Dim count as Integer
Dim s as String
s="Dan*Smith*"
count=CountFields(s, "*")


This example in the Open event handler populates a PopupMenu and sets the initial value to the current month:

Dim s as String
Dim i,last as Integer
Dim d as New Date
s="January,February,March,April,May,June,July,"_
+"August,September,October,November,December"
last=CountFields(s,",")
For i=1 to last
me.addRow NthField(s,",",i)
Next
me.ListIndex=d.Month-1

See Also

CountFieldsB, NthField, Split functions; TextInputStream object example.

Personal tools
Namespaces

Variants
Actions
Main
Content
Starting Out
Dig Deeper
More Help
Toolbox
About