String.CountFields

From Xojo Documentation

Revision as of 21:10, 22 November 2019 by Gperlman (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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 a MemoryBlock instead.

Usage

result = stringVariable.CountFields(separator)

Part Type Description
result Integer The number of values in source that are separated by separator.
stringVariable 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 fields are separated with a specific character or characters.

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

fa-info-circle-32.png
Using CountFields in a loop to extract fields from a string is inefficient. You should use Split and Ubound for this purpose.

Sample Code

The code below returns 5.

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

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

Var count As Integer
Var s As String
s = "Dan*Smith*"
count = s.CountFields("*")

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

Var s As String
Var i, last As Integer
Var d As New DateTime = DateTime.Now
s = "January,February,March,April,May,June,July," _
+ "August,September,October,November,December"
last = s.CountFields(",")
For i = 1 To last
Me.AddRow(s.NthField(",", i))
Next
Me.SelectedRowIndex = d.Month - 1

See Also

String.NthField, String.Split, Text.Split functions; TextInputStream class