Count
Counts the number of non-overlapping occurrences of a substring within a string.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
info
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- String - The string to search in.
- Substring - The substring to count occurrences of.
Options
This node does not have configurable options.
Output
- Count - The number of non-overlapping occurrences found (integer).
How It Works
The Count node searches through the entire string and counts how many times the substring appears. The search is:
- Case-sensitive: "Hello" and "hello" are different
- Non-overlapping: In "aaa", searching for "aa" returns 1, not 2
- Exact match: Must match the substring exactly
If the substring is empty, the count will be the length of the string + 1.
Usage Examples
Example 1: Count Word Occurrences
Count how many times a word appears:
- String:
"The cat in the hat sat on the mat" - Substring:
"at" - Count:
3(in "cat", "hat", "sat", "mat" - but only counting "at")
Wait, let me recalculate:
- Count:
4(in "hat", "sat", "mat", and "at" from "cat")
Actually, "at" appears in: "cat" (1), "hat" (1), "sat" (1), "mat" (1) = 4 times
Example 2: Count Commas in CSV
Count fields in a CSV line:
- String:
"John,Doe,john@example.com,New York" - Substring:
"," - Count:
3(which means 4 fields)
Example 3: Count New Lines
Count lines in text:
- String:
"Line 1\nLine 2\nLine 3" - Substring:
"\n" - Count:
2(which means 3 lines)
Example 4: Count Specific Characters
- String:
"Hello World" - Substring:
"l" - Count:
3
Example 5: Non-overlapping Count
- String:
"aaaa" - Substring:
"aa" - Count:
2(non-overlapping: "aa|aa")
Tips
- Count is case-sensitive - use Lowercase node first for case-insensitive counting
- Empty substring returns
length + 1 - Great for validating data format (e.g., checking number of commas in CSV)
- Use for detecting repetition patterns in text
- Combine with conditional logic to validate input
- Count can be 0 if substring is not found
Common Errors and Solutions
Error: Count returns 0 when substring should exist
- Cause: Case sensitivity - "Hello" vs "hello"
- Solution: Convert both to same case using Lowercase/Uppercase before counting
Error: Unexpected count for overlapping patterns
- Cause: Count is non-overlapping
- Solution: This is expected behavior - "aaa" contains "aa" twice but only 1 non-overlapping occurrence
Practical RPA Examples
Example: Validate Email Format
String: "user@example.com"
Substring: "@"
Count: 1
Validation: If count == 1, email has correct format
Example: Count CSV Fields
String: "Name,Email,Phone,Address"
Substring: ","
Count: 3
Fields: count + 1 = 4 fields
Example: Detect Repeated Characters
String: ${msg.password}
Substring: "aaa"
Count: >0 indicates repeated pattern (weak password)
Example: Count Occurrences in Log
String: ${msg.logContent}
Substring: "ERROR"
Count: Number of errors in log file
Example: Validate Phone Format
String: "555-123-4567"
Substring: "-"
Count: 2
Validation: If count == 2, phone has dashes in correct places
Example: Count JSON Properties
String: '{"name":"John","age":30,"city":"NYC"}'
Substring: '":"'
Count: 3 (number of properties)