Skip to main content
Glama

find_nth_char

Locate the position of the nth occurrence of a specific character within text. Returns the index or -1 if not found.

Instructions

Find index of nth occurrence of a character. Returns -1 if not found.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
textYes
charYes
nNo

Implementation Reference

  • The handler function for the 'find_nth_char' tool, including decorator for registration and inline schema via Annotated types. Implements iteration over text to count and locate the nth occurrence of the specified character.
    @mcp.tool() def find_nth_char( text: Annotated[str, "Text to search in"], char: Annotated[str, "Single character to find"], n: Annotated[int, "Which occurrence to find (1-based)"] = 1 ) -> int: """Find index of nth occurrence of a character. Returns -1 if not found.""" if len(char) != 1: raise ValueError("char must be a single character") if n < 1: raise ValueError("n must be >= 1") count = 0 for i, c in enumerate(text): if c == char: count += 1 if count == n: return i return -1

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/agent-hanju/char-index-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server