<div class="meta" robots="noindex">

</div>

Method

# CountFields

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<div class="warning">

<div class="title">

Warning

</div>

This item was deprecated in version 2019r2. Please use `String.CountFields<string.countfields>` as a replacement.

</div>

## Description

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 `MemoryBlock</api/language/memoryblock>` instead.

## Usage

*result* = **CountFields**(*stringVariable*, *separator*)

or

*result* = *stringVariable*.**CountFields**(*separator*)

| Part           | Type                               | Description                                                         |
|----------------|------------------------------------|---------------------------------------------------------------------|
| result         | `Integer</api/data_types/integer>` | The number of values in *source* that are separated by *separator*. |
| stringVariable | `String</api/data_types/string>`   | The original string.                                                |
| separator      | `String</api/data_types/string>`   | The character or characters that separate the values in *source*.   |

## Notes

The <span class="title-ref">CountFields</span> 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*, <span class="title-ref">CountFields</span> returns 1. If *source* is null, <span class="title-ref">CountFields</span> returns zero.

<div class="note">

<div class="title">

Note

</div>

Using <span class="title-ref">CountFields</span> in a loop to extract fields from a string is inefficient. You should use `String.Split<string.split>` and `Arrays.LastIndex<arrays.lastindex>` for this purpose.

</div>

## Sample code

The code below returns 5.

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

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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

``` xojo
Dim count As Integer
Dim s As String
s = "Dan*Smith*"
count = CountFields(s, "*")
```

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

This code in the `Open<control.open>` event handler populates a `PopupMenu</api/deprecated/popupmenu>` and sets the initial value to the current month:

``` xojo
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
```

## Compatibility

All project types on all supported operating systems.

## See also

`MemoryBlock</api/language/memoryblock>`, `String.NthField<string.nthfield>`, `String.Split<string.split>`, `Text.Split<text.split>` functions; `TextInputStream</api/files/textinputstream>` class
