String Functions in Nim
The standard library’s strutils
module provides many useful string-related functions. Here are some examples to give you a sense of the module.
When you run this program, you’ll see the following output:
In this Nim version, we’ve used equivalent functions from the strutils
module. Some notable differences:
- Nim uses
contains
instead of Contains
, following its naming convention. HasPrefix
and HasSuffix
are replaced with startsWith
and endsWith
.Index
is replaced with find
.- The
Replace
function in Nim replaces all occurrences by default. To replace only one occurrence, we specify the count as the last argument. - Nim’s
split
function returns a sequence (seq
) instead of a slice.
These functions demonstrate Nim’s powerful string manipulation capabilities, which are similar to those found in other modern programming languages.