On this page
std/strutils
Source EditThe system module defines several common functions for working with strings, such as:
$
for converting other data-types to strings&
for string concatenationadd
for adding a new character or a string to the existing onein
(alias forcontains
) andnotin
for checking if a character is in a string
This module builds upon that, providing additional functionality in form of procedures, iterators and templates for strings.
Example:
import std/strutils
let
numbers = @[867, 5309]
multiLineString = "first line\nsecond line\nthird line"
let jenny = numbers.join("-")
assert jenny == "867-5309"
assert splitLines(multiLineString) ==
@["first line", "second line", "third line"]
assert split(multiLineString) == @["first", "line", "second",
"line", "third", "line"]
assert indent(multiLineString, 4) ==
" first line\n second line\n third line"
assert 'z'.repeat(5) == "zzzzz"
The chaining of functions is possible thanks to the method call syntax:
Example:
import std/strutils
from std/sequtils import map
let jenny = "867-5309"
assert jenny.split('-').map(parseInt) == @[867, 5309]
assert "Beetlejuice".indent(1).repeat(3).strip ==
"Beetlejuice Beetlejuice Beetlejuice"
This module is available for the JavaScript target.
See also:
- strformat module for string interpolation and formatting
- unicode module for Unicode UTF-8 handling
- sequtils module for operations on container types (including strings)
- parsecsv module for a high-performance CSV parser
- parseutils module for lower-level parsing of tokens, numbers, identifiers, etc.
- parseopt module for command-line parsing
- pegs module for PEG (Parsing Expression Grammar) support
- strtabs module for efficient hash tables (dictionaries, in some programming languages) mapping from strings to strings
- ropes module for rope data type, which can represent very long strings efficiently
- re module for regular expression (regex) support
- strscans for
scanf
andscanp
macros, which offer easier substring extraction than regular expressions
Imports
Types
Consts
-
AllChars = {'\x00'..'\xFF'}
-
A set with all the possible characters.
Not very useful by its own, you can use it to create inverted sets to make the find func find invalid characters in strings. Example:
Source Editlet invalid = AllChars - Digits doAssert "01234".find(invalid) == -1 doAssert "01A34".find(invalid) == 2
-
IdentChars = {'a'..'z', 'A'..'Z', '0'..'9', '_'}
- The set of characters an identifier can consist of. Source Edit
-
IdentStartChars = {'a'..'z', 'A'..'Z', '_'}
- The set of characters an identifier can start with. Source Edit
-
Newlines = {'\r', '\n'}
- The set of characters a newline terminator can start with (carriage return, line feed). Source Edit
-
PrintableChars = {'\t'..'\r', ' '..'~'}
- The set of all printable ASCII characters (letters, digits, whitespace, and punctuation characters). Source Edit
Procs
-
func `%`(formatstr, a: string): string {....gcsafe, extern: "nsuFormatSingleElem", raises: [ValueError], tags: [], forbids: [].}
-
This is the same as
formatstr % [a]
(see % func). Source Edit -
func `%`(formatstr: string; a: openArray[string]): string {....gcsafe, extern: "nsuFormatOpenArray", raises: [ValueError], tags: [], forbids: [].}
-
Interpolates a format string with the values from
a
.The substitution operator performs string substitutions in
formatstr
and returns a modifiedformatstr
. This is often called string interpolation.This is best explained by an example:
"$1 eats $2." % ["The cat", "fish"]
Results in:
"The cat eats fish."
The substitution variables (the thing after the
$
) are enumerated from 1 toa.len
. To produce a verbatim$
, use$$
. The notation$#
can be used to refer to the next substitution variable:"$# eats $#." % ["The cat", "fish"]
Substitution variables can also be words (that is
[A-Za-z_]+[A-Za-z0-9_]*
) in which case the arguments ina
with even indices are keys and with odd indices are the corresponding values. An example:"$animal eats $food." % ["animal", "The cat", "food", "fish"]
Results in:
"The cat eats fish."
The variables are compared with
cmpIgnoreStyle
.ValueError
is raised if an ill-formed format string has been passed to the%
operator.See also:
- strformat module for string interpolation and formatting
-
func abbrev(s: string; possibilities: openArray[string]): int {....raises: [], tags: [], forbids: [].}
-
Returns the index of the first item in
possibilities
which starts withs
, if not ambiguous.Returns -1 if no item has been found and -2 if multiple items match.
Example:
Source EditdoAssert abbrev("fac", ["college", "faculty", "industry"]) == 1 doAssert abbrev("foo", ["college", "faculty", "industry"]) == -1 # Not found doAssert abbrev("fac", ["college", "faculty", "faculties"]) == -2 # Ambiguous doAssert abbrev("college", ["college", "colleges", "industry"]) == 0
-
func addSep(dest: var string; sep = ", "; startLen: Natural = 0) {.inline, ...raises: [], tags: [], forbids: [].}
-
Adds a separator to
dest
only if its length is bigger thanstartLen
.A shorthand for:
if dest.len > startLen: add(dest, sep)
This is often useful for generating some code where the items need to be separated by
sep
.sep
is only added ifdest
is longer thanstartLen
. The following example creates a string describing an array of integers.Example:
Source Editvar arr = "[" for x in items([2, 3, 5, 7, 11]): addSep(arr, startLen = len("[")) add(arr, $x) add(arr, "]") doAssert arr == "[2, 3, 5, 7, 11]"
-
func align(s: string; count: Natural; padding = ' '): string {....gcsafe, extern: "nsuAlignString", raises: [], tags: [], forbids: [].}
-
Aligns a string
s
withpadding
, so that it is of lengthcount
.padding
characters (by default spaces) are added befores
resulting in right alignment. Ifs.len >= count
, no spaces are added ands
is returned unchanged. If you need to left align a string use the alignLeft func.See also:
Example:
Source Editassert align("abc", 4) == " abc" assert align("a", 0) == "a" assert align("1232", 6) == " 1232" assert align("1232", 6, '#') == "##1232"
-
func alignLeft(s: string; count: Natural; padding = ' '): string {....raises: [], tags: [], forbids: [].}
-
Left-Aligns a string
s
withpadding
, so that it is of lengthcount
.padding
characters (by default spaces) are added afters
resulting in left alignment. Ifs.len >= count
, no spaces are added ands
is returned unchanged. If you need to right align a string use the align func.See also:
Example:
Source Editassert alignLeft("abc", 4) == "abc " assert alignLeft("a", 0) == "a" assert alignLeft("1232", 6) == "1232 " assert alignLeft("1232", 6, '#') == "1232##"
-
func capitalizeAscii(s: string): string {....gcsafe, extern: "nsuCapitalizeAscii", raises: [], tags: [], forbids: [].}
-
Converts the first character of string
s
into upper case.This works only for the letters
A-Z
. Use Unicode module for UTF-8 support.See also:
Example:
Source EditdoAssert capitalizeAscii("foo") == "Foo" doAssert capitalizeAscii("-bar") == "-bar"
-
func center(s: string; width: int; fillChar: char = ' '): string {....gcsafe, extern: "nsuCenterString", raises: [], tags: [], forbids: [].}
-
Return the contents of
s
centered in a stringwidth
long usingfillChar
(default: space) as padding.The original string is returned if
width
is less than or equal tos.len
.See also:
Example:
Source Editlet a = "foo" doAssert a.center(2) == "foo" doAssert a.center(5) == " foo " doAssert a.center(6) == " foo "
-
func cmpIgnoreCase(a, b: string): int {....gcsafe, extern: "nsuCmpIgnoreCase", raises: [], tags: [], forbids: [].}
-
Compares two strings in a case insensitive manner. Returns:
0 if a == b
< 0 if a < b0 if a > b
Example:
Source EditdoAssert cmpIgnoreCase("FooBar", "foobar") == 0 doAssert cmpIgnoreCase("bar", "Foo") < 0 doAssert cmpIgnoreCase("Foo5", "foo4") > 0
-
func cmpIgnoreStyle(a, b: string): int {....gcsafe, extern: "nsuCmpIgnoreStyle", raises: [], tags: [], forbids: [].}
-
Semantically the same as
cmp(normalize(a), normalize(b))
. It is just optimized to not allocate temporary strings. This should NOT be used to compare Nim identifier names. Use macros.eqIdent for that.Returns:
0 if a == b
< 0 if a < b0 if a > b
Example:
Source EditdoAssert cmpIgnoreStyle("foo_bar", "FooBar") == 0 doAssert cmpIgnoreStyle("foo_bar_5", "FooBar4") > 0
-
func continuesWith(s, substr: string; start: Natural): bool {....gcsafe, extern: "nsuContinuesWith", raises: [], tags: [], forbids: [].}
-
Returns true if
s
continues withsubstr
at positionstart
.If
substr == ""
true is returned.See also:
Example:
Source Editlet a = "abracadabra" doAssert a.continuesWith("ca", 4) == true doAssert a.continuesWith("ca", 5) == false doAssert a.continuesWith("dab", 6) == true
-
func count(s: string; sub: char): int {....gcsafe, extern: "nsuCountChar", raises: [], tags: [], forbids: [].}
-
Counts the occurrences of the character
sub
in the strings
.See also:
Source Edit -
func count(s: string; sub: string; overlapping: bool = false): int {....gcsafe, extern: "nsuCountString", raises: [], tags: [], forbids: [].}
-
Counts the occurrences of a substring
sub
in the strings
. Overlapping occurrences ofsub
only count whenoverlapping
is set to true (default: false).See also:
Source Edit -
func countLines(s: string): int {....gcsafe, extern: "nsuCountLines", raises: [], tags: [], forbids: [].}
-
Returns the number of lines in the string
s
.This is the same as
len(splitLines(s))
, but much more efficient because it doesn't modify the string creating temporary objects. Every character literal newline combination (CR, LF, CR-LF) is supported.In this context, a line is any string separated by a newline combination. A line can be an empty string.
See also:
Example:
Source EditdoAssert countLines("First line\l and second line.") == 2
-
func dedent(s: string; count: Natural = indentation(s)): string {....gcsafe, extern: "nsuDedent", raises: [], tags: [], forbids: [].}
-
Unindents each line in
s
bycount
amount ofpadding
. The only difference between this and the unindent func is that this by default only cuts off the amount of indentation that all lines ofs
share as opposed to all indentation. It only supports spaces as padding.Note: This does not preserve the new line characters used in
s
.See also:
Example:
Source Editlet x = """ Hello There """.dedent() doAssert x == "Hello\n There\n"
-
func delete(s: var string; first, last: int) {....gcsafe, extern: "nsuDelete", deprecated: "use `delete(s, first..last)`", raises: [], tags: [], forbids: [].}
-
s
the characters at positionsfirst .. last
(both ends included).Example: cmd: --warning:deprecated:off
Source Editvar a = "abracadabra" a.delete(4, 5) doAssert a == "abradabra" a.delete(1, 6) doAssert a == "ara" a.delete(2, 999) doAssert a == "ar"
Deletes in -
func delete(s: var string; slice: Slice[int]) {....raises: [], tags: [], forbids: [].}
-
Deletes the items
s[slice]
, raisingIndexDefect
if the slice contains elements out of range.This operation moves all elements after
s[slice]
in linear time, and is the string analog tosequtils.delete
.Example:
Source Editvar a = "abcde" doAssertRaises(IndexDefect): a.delete(4..5) assert a == "abcde" a.delete(4..4) assert a == "abcd" a.delete(1..2) assert a == "ad" a.delete(1..<1) # empty slice assert a == "ad"
-
func endsWith(s, suffix: string): bool {....gcsafe, extern: "nsuEndsWith", raises: [], tags: [], forbids: [].}
-
Returns true if
s
ends withsuffix
.If
suffix == ""
true is returned.See also:
Example:
Source Editlet a = "abracadabra" doAssert a.endsWith("abra") == true doAssert a.endsWith("dab") == false
-
func escape(s: string; prefix = "\""; suffix = "\""): string {....gcsafe, extern: "nsuEscape", raises: [], tags: [], forbids: [].}
-
Escapes a string
s
.Note: The escaping scheme is different fromsystem.addEscapedChar
.- replaces
'\0'..'\31'
and'\127'..'\255'
by\xHH
whereHH
is its hexadecimal value - replaces
\
by\\
- replaces
'
by\'
- replaces
"
by\"
The resulting string is prefixed with
prefix
and suffixed withsuffix
. Both may be empty strings.See also:
- addEscapedChar proc
- unescape func for the opposite operation
- replaces
-
func find(a: SkipTable; s, sub: string; start: Natural = 0; last = -1): int {. ...gcsafe, extern: "nsuFindStrA", raises: [], tags: [], forbids: [].}
-
Searches for
sub
ins
inside rangestart..last
using preprocessed tablea
. Iflast
is unspecified, it defaults tos.high
(the last element).Searching is case-sensitive. If
sub
is not ins
, -1 is returned.See also:
Source Edit -
func find(s, sub: string; start: Natural = 0; last = -1): int {....gcsafe, extern: "nsuFindStr", raises: [], tags: [], forbids: [].}
-
Searches for
sub
ins
inside rangestart..last
(both ends included). Iflast
is unspecified or negative, it defaults tos.high
(the last element).Searching is case-sensitive. If
sub
is not ins
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func find(s: string; chars: set[char]; start: Natural = 0; last = -1): int {. ...gcsafe, extern: "nsuFindCharSet", raises: [], tags: [], forbids: [].}
-
Searches for
chars
ins
inside rangestart..last
(both ends included). Iflast
is unspecified or negative, it defaults tos.high
(the last element).If
s
contains none of the characters inchars
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func find(s: string; sub: char; start: Natural = 0; last = -1): int {....gcsafe, extern: "nsuFindChar", raises: [], tags: [], forbids: [].}
-
Searches for
sub
ins
inside rangestart..last
(both ends included). Iflast
is unspecified or negative, it defaults tos.high
(the last element).Searching is case-sensitive. If
sub
is not ins
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func format(formatstr: string; a: varargs[string, `$`]): string {....gcsafe, extern: "nsuFormatVarargs", raises: [ValueError], tags: [], forbids: [].}
-
This is the same as
formatstr % a
(see % func) except that it supports auto stringification.See also:
- strformat module for string interpolation and formatting
-
func formatBiggestFloat(f: BiggestFloat; format: FloatFormatMode = ffDefault; precision: range[-1 .. 32] = 16; decimalSep = '.'): string {. ...gcsafe, extern: "nsu$1", raises: [], tags: [], forbids: [].}
-
Converts a floating point value
f
to a string.If
format == ffDecimal
then precision is the number of digits to be printed after the decimal point. Ifformat == ffScientific
then precision is the maximum number of significant digits to be printed.precision
's default value is the maximum number of meaningful digits after the decimal point for Nim'sbiggestFloat
type.If
precision == -1
, it tries to format it nicely.Example:
Source Editlet x = 123.456 doAssert x.formatBiggestFloat() == "123.4560000000000" doAssert x.formatBiggestFloat(ffDecimal, 4) == "123.4560" doAssert x.formatBiggestFloat(ffScientific, 2) == "1.23e+02"
-
func formatEng(f: BiggestFloat; precision: range[0 .. 32] = 10; trim: bool = true; siPrefix: bool = false; unit: string = ""; decimalSep = '.'; useUnitSpace = false): string {....raises: [], tags: [], forbids: [].}
-
Converts a floating point value
f
to a string using engineering notation.Numbers in of the range -1000.0<f<1000.0 will be formatted without an exponent. Numbers outside of this range will be formatted as a significand in the range -1000.0<f<1000.0 and an exponent that will always be an integer multiple of 3, corresponding with the SI prefix scale k, M, G, T etc for numbers with an absolute value greater than 1 and m, μ, n, p etc for numbers with an absolute value less than 1.
The default configuration (
trim=true
andprecision=10
) shows the shortest form that precisely (up to a maximum of 10 decimal places) displays the value. For example, 4.100000 will be displayed as 4.1 (which is mathematically identical) whereas 4.1000003 will be displayed as 4.1000003.If
trim
is set to true, trailing zeros will be removed; if false, the number of digits specified byprecision
will always be shown.precision
can be used to set the number of digits to be shown after the decimal point or (iftrim
is true) the maximum number of digits to be shown.formatEng(0, 2, trim=false) == "0.00" formatEng(0, 2) == "0" formatEng(0.053, 0) == "53e-3" formatEng(52731234, 2) == "52.73e6" formatEng(-52731234, 2) == "-52.73e6"
If
siPrefix
is set to true, the number will be displayed with the SI prefix corresponding to the exponent. For example 4100 will be displayed as "4.1 k" instead of "4.1e3". Note thatu
is used for micro- in place of the greek letter mu (μ) as per ISO 2955. Numbers with an absolute value outside of the range 1e-18<f<1000e18 (1a<f<1000E) will be displayed with an exponent rather than an SI prefix, regardless of whethersiPrefix
is true.If
useUnitSpace
is true, the provided unit will be appended to the string (with a space as required by the SI standard). This behaviour is slightly different to appending the unit to the result as the location of the space is altered depending on whether there is an exponent.formatEng(4100, siPrefix=true, unit="V") == "4.1 kV" formatEng(4.1, siPrefix=true, unit="V") == "4.1 V" formatEng(4.1, siPrefix=true) == "4.1" # Note lack of space formatEng(4100, siPrefix=true) == "4.1 k" formatEng(4.1, siPrefix=true, unit="") == "4.1 " # Space with unit="" formatEng(4100, siPrefix=true, unit="") == "4.1 k" formatEng(4100) == "4.1e3" formatEng(4100, unit="V") == "4.1e3 V" formatEng(4100, unit="", useUnitSpace=true) == "4.1e3 " # Space with useUnitSpace=true
decimalSep
is used as the decimal separator.See also:
- strformat module for string interpolation and formatting
-
func formatFloat(f: float; format: FloatFormatMode = ffDefault; precision: range[-1 .. 32] = 16; decimalSep = '.'): string {. ...gcsafe, extern: "nsu$1", raises: [], tags: [], forbids: [].}
-
Converts a floating point value
f
to a string.If
format == ffDecimal
then precision is the number of digits to be printed after the decimal point. Ifformat == ffScientific
then precision is the maximum number of significant digits to be printed.precision
's default value is the maximum number of meaningful digits after the decimal point for Nim'sfloat
type.If
precision == -1
, it tries to format it nicely.Example:
Source Editlet x = 123.456 doAssert x.formatFloat() == "123.4560000000000" doAssert x.formatFloat(ffDecimal, 4) == "123.4560" doAssert x.formatFloat(ffScientific, 2) == "1.23e+02"
-
func formatSize(bytes: int64; decimalSep = '.'; prefix = bpIEC; includeSpace = false): string {....raises: [], tags: [], forbids: [].}
-
Rounds and formats
bytes
.By default, uses the IEC/ISO standard binary prefixes, so 1024 will be formatted as 1KiB. Set prefix to
bpColloquial
to use the colloquial names from the SI standard (e.g. k for 1000 being reused as 1024).includeSpace
can be set to true to include the (SI preferred) space between the number and the unit (e.g. 1 KiB).See also:
- strformat module for string interpolation and formatting
Example:
Source EditdoAssert formatSize((1'i64 shl 31) + (300'i64 shl 20)) == "2.293GiB" doAssert formatSize((2.234*1024*1024).int) == "2.234MiB" doAssert formatSize(4096, includeSpace = true) == "4 KiB" doAssert formatSize(4096, prefix = bpColloquial, includeSpace = true) == "4 kB" doAssert formatSize(4096) == "4KiB" doAssert formatSize(5_378_934, prefix = bpColloquial, decimalSep = ',') == "5,13MB"
-
func fromBin[T: SomeInteger](s: string): T
-
Parses a binary integer value from a string
s
.If
s
is not a valid binary integer,ValueError
is raised.s
can have one of the following optional prefixes:0b
,0B
. Underscores withins
are ignored.Does not check for overflow. If the value represented by
s
is too big to fit into a return type, only the value of the rightmost binary digits ofs
is returned without producing an error.Example:
Source Editlet s = "0b_0100_1000_1000_1000_1110_1110_1001_1001" doAssert fromBin[int](s) == 1216933529 doAssert fromBin[int8](s) == 0b1001_1001'i8 doAssert fromBin[int8](s) == -103'i8 doAssert fromBin[uint8](s) == 153 doAssert s.fromBin[:int16] == 0b1110_1110_1001_1001'i16 doAssert s.fromBin[:uint64] == 1216933529'u64
-
func fromHex[T: SomeInteger](s: string): T
-
Parses a hex integer value from a string
s
.If
s
is not a valid hex integer,ValueError
is raised.s
can have one of the following optional prefixes:0x
,0X
,#
. Underscores withins
are ignored.Does not check for overflow. If the value represented by
s
is too big to fit into a return type, only the value of the rightmost hex digits ofs
is returned without producing an error.Example:
Source Editlet s = "0x_1235_8df6" doAssert fromHex[int](s) == 305499638 doAssert fromHex[int8](s) == 0xf6'i8 doAssert fromHex[int8](s) == -10'i8 doAssert fromHex[uint8](s) == 246'u8 doAssert s.fromHex[:int16] == -29194'i16 doAssert s.fromHex[:uint64] == 305499638'u64
-
func fromOct[T: SomeInteger](s: string): T
-
Parses an octal integer value from a string
s
.If
s
is not a valid octal integer,ValueError
is raised.s
can have one of the following optional prefixes:0o
,0O
. Underscores withins
are ignored.Does not check for overflow. If the value represented by
s
is too big to fit into a return type, only the value of the rightmost octal digits ofs
is returned without producing an error.Example:
Source Editlet s = "0o_123_456_777" doAssert fromOct[int](s) == 21913087 doAssert fromOct[int8](s) == 0o377'i8 doAssert fromOct[int8](s) == -1'i8 doAssert fromOct[uint8](s) == 255'u8 doAssert s.fromOct[:int16] == 24063'i16 doAssert s.fromOct[:uint64] == 21913087'u64
-
func indent(s: string; count: Natural; padding: string = " "): string {....gcsafe, extern: "nsuIndent", raises: [], tags: [], forbids: [].}
-
Indents each line in
s
bycount
amount ofpadding
.Note: This does not preserve the new line characters used in
s
.See also:
Example:
Source EditdoAssert indent("First line\c\l and second line.", 2) == " First line\l and second line."
-
func initSkipTable(a: var SkipTable; sub: string) {....gcsafe, extern: "nsuInitSkipTable", raises: [], tags: [], forbids: [].}
-
Initializes table
a
for efficient search of substringsub
.See also:
Source Edit -
func insertSep(s: string; sep = '_'; digits = 3): string {....gcsafe, extern: "nsuInsertSep", raises: [], tags: [], forbids: [].}
-
Inserts the separator
sep
afterdigits
characters (default: 3) from right to left.Even though the algorithm works with any string
s
, it is only useful ifs
contains a number.Example:
Source EditdoAssert insertSep("1000000") == "1_000_000"
-
func intToStr(x: int; minchars: Positive = 1): string {....gcsafe, extern: "nsuIntToStr", raises: [], tags: [], forbids: [].}
-
Converts
x
to its decimal representation.The resulting string will be minimally
minchars
characters long. This is achieved by adding leading zeros.Example:
Source EditdoAssert intToStr(1984) == "1984" doAssert intToStr(1984, 6) == "001984"
-
func isAlphaAscii(c: char): bool {....gcsafe, extern: "nsuIsAlphaAsciiChar", raises: [], tags: [], forbids: [].}
-
Checks whether or not character
c
is alphabetical.This checks a-z, A-Z ASCII characters only. Use Unicode module for UTF-8 support.
Example:
Source EditdoAssert isAlphaAscii('e') == true doAssert isAlphaAscii('E') == true doAssert isAlphaAscii('8') == false
-
func isAlphaNumeric(c: char): bool {....gcsafe, extern: "nsuIsAlphaNumericChar", raises: [], tags: [], forbids: [].}
-
Checks whether or not
c
is alphanumeric.This checks a-z, A-Z, 0-9 ASCII characters only.
Example:
Source EditdoAssert isAlphaNumeric('n') == true doAssert isAlphaNumeric('8') == true doAssert isAlphaNumeric(' ') == false
-
func isLowerAscii(c: char): bool {....gcsafe, extern: "nsuIsLowerAsciiChar", raises: [], tags: [], forbids: [].}
-
Checks whether or not
c
is a lower case character.This checks ASCII characters only. Use Unicode module for UTF-8 support.
See also:
Example:
Source EditdoAssert isLowerAscii('e') == true doAssert isLowerAscii('E') == false doAssert isLowerAscii('7') == false
-
func isUpperAscii(c: char): bool {....gcsafe, extern: "nsuIsUpperAsciiChar", raises: [], tags: [], forbids: [].}
-
Checks whether or not
c
is an upper case character.This checks ASCII characters only. Use Unicode module for UTF-8 support.
See also:
Example:
Source EditdoAssert isUpperAscii('e') == false doAssert isUpperAscii('E') == true doAssert isUpperAscii('7') == false
-
func multiReplace(s: string; replacements: varargs[(string, string)]): string {. ...raises: [], tags: [], forbids: [].}
-
Same as replace, but specialized for doing multiple replacements in a single pass through the input string.
multiReplace
performs all replacements in a single pass, this means it can be used to swap the occurrences of "a" and "b", for instance.If the resulting string is not longer than the original input string, only a single memory allocation is required.
The order of the replacements does matter. Earlier replacements are preferred over later replacements in the argument list.
Source Edit -
func nimIdentNormalize(s: string): string {....raises: [], tags: [], forbids: [].}
-
Normalizes the string
s
as a Nim identifier.That means to convert to lower case and remove any '_' on all characters except first one.
Warning: Backticks (`) are not handled: they remain as is and spaces are preserved. See nimIdentBackticksNormalize for an alternative approach.Example:
Source EditdoAssert nimIdentNormalize("Foo_bar") == "Foobar"
-
func normalize(s: string): string {....gcsafe, extern: "nsuNormalize", raises: [], tags: [], forbids: [].}
-
Normalizes the string
s
.That means to convert it to lower case and remove any '_'. This should NOT be used to normalize Nim identifier names.
See also:
Example:
Source EditdoAssert normalize("Foo_bar") == "foobar" doAssert normalize("Foo Bar") == "foo bar"
-
func parseBinInt(s: string): int {....gcsafe, extern: "nsuParseBinInt", raises: [ValueError], tags: [], forbids: [].}
-
Parses a binary integer value contained in
s
.If
s
is not a valid binary integer,ValueError
is raised.s
can have one of the following optional prefixes:0b
,0B
. Underscores withins
are ignored.Example:
Source Editlet a = "0b11_0101" b = "111" doAssert a.parseBinInt() == 53 doAssert b.parseBinInt() == 7
-
func parseBool(s: string): bool {....raises: [ValueError], tags: [], forbids: [].}
-
Parses a value into a
bool
.If
s
is one of the following values:y, yes, true, 1, on
, then returnstrue
. Ifs
is one of the following values:n, no, false, 0, off
, then returnsfalse
. Ifs
is something else aValueError
exception is raised.Example:
Source Editlet a = "n" doAssert parseBool(a) == false
-
func parseEnum[T: enum](s: string): T
-
Parses an enum
T
. This errors at compile time, if the given enum type contains multiple fields with the same string value.Raises
ValueError
for an invalid value ins
. The comparison is done in a style insensitive way.Example:
Source Edittype MyEnum = enum first = "1st", second, third = "3rd" doAssert parseEnum[MyEnum]("1_st") == first doAssert parseEnum[MyEnum]("second") == second doAssertRaises(ValueError): echo parseEnum[MyEnum]("third")
-
func parseEnum[T: enum](s: string; default: T): T
-
Parses an enum
T
. This errors at compile time, if the given enum type contains multiple fields with the same string value.Uses
default
for an invalid value ins
. The comparison is done in a style insensitive way.Example:
Source Edittype MyEnum = enum first = "1st", second, third = "3rd" doAssert parseEnum[MyEnum]("1_st") == first doAssert parseEnum[MyEnum]("second") == second doAssert parseEnum[MyEnum]("last", third) == third
-
func parseFloat(s: string): float {....gcsafe, extern: "nsuParseFloat", raises: [ValueError], tags: [], forbids: [].}
-
Parses a decimal floating point value contained in
s
.If
s
is not a valid floating point number,ValueError
is raised.NAN
,INF
,-INF
are also supported (case insensitive comparison).Example:
Source EditdoAssert parseFloat("3.14") == 3.14 doAssert parseFloat("inf") == 1.0/0
-
func parseHexInt(s: string): int {....gcsafe, extern: "nsuParseHexInt", raises: [ValueError], tags: [], forbids: [].}
-
Parses a hexadecimal integer value contained in
s
.If
Source Edits
is not a valid hex integer,ValueError
is raised.s
can have one of the following optional prefixes:0x
,0X
,#
. Underscores withins
are ignored. -
func parseHexStr(s: string): string {....gcsafe, extern: "nsuParseHexStr", raises: [ValueError], tags: [], forbids: [].}
-
Converts hex-encoded string to byte string, e.g.:
Raises
ValueError
for an invalid hex values. The comparison is case-insensitive.See also:
- toHex func for the reverse operation
Example:
Source Editlet a = "41" b = "3161" c = "00ff" doAssert parseHexStr(a) == "A" doAssert parseHexStr(b) == "1a" doAssert parseHexStr(c) == "\0\255"
-
func parseOctInt(s: string): int {....gcsafe, extern: "nsuParseOctInt", raises: [ValueError], tags: [], forbids: [].}
-
Parses an octal integer value contained in
s
.If
Source Edits
is not a valid oct integer,ValueError
is raised.s
can have one of the following optional prefixes:0o
,0O
. Underscores withins
are ignored. -
func removePrefix(s: var string; c: char) {....gcsafe, extern: "nsuRemovePrefixChar", raises: [], tags: [], forbids: [].}
-
Removes all occurrences of a single character (in-place) from the start of a string.
See also:
Example:
Source Editvar ident = "pControl" ident.removePrefix('p') doAssert ident == "Control"
-
func removePrefix(s: var string; chars: set[char] = Newlines) {....gcsafe, extern: "nsuRemovePrefixCharSet", raises: [], tags: [], forbids: [].}
-
Removes all characters from
chars
from the start of the strings
(in-place).See also:
Example:
Source Editvar userInput = "\r\n*~Hello World!" userInput.removePrefix doAssert userInput == "*~Hello World!" userInput.removePrefix({'~', '*'}) doAssert userInput == "Hello World!" var otherInput = "?!?Hello!?!" otherInput.removePrefix({'!', '?'}) doAssert otherInput == "Hello!?!"
-
func removeSuffix(s: var string; c: char) {....gcsafe, extern: "nsuRemoveSuffixChar", raises: [], tags: [], forbids: [].}
-
Removes all occurrences of a single character (in-place) from the end of a string.
See also:
Example:
Source Editvar table = "users" table.removeSuffix('s') doAssert table == "user" var dots = "Trailing dots......." dots.removeSuffix('.') doAssert dots == "Trailing dots"
-
func removeSuffix(s: var string; chars: set[char] = Newlines) {....gcsafe, extern: "nsuRemoveSuffixCharSet", raises: [], tags: [], forbids: [].}
-
Removes all characters from
chars
from the end of the strings
(in-place).See also:
Example:
Source Editvar userInput = "Hello World!*~\r\n" userInput.removeSuffix doAssert userInput == "Hello World!*~" userInput.removeSuffix({'~', '*'}) doAssert userInput == "Hello World!" var otherInput = "Hello!?!" otherInput.removeSuffix({'!', '?'}) doAssert otherInput == "Hello"
-
func replace(s, sub: string; by = ""): string {....gcsafe, extern: "nsuReplaceStr", raises: [], tags: [], forbids: [].}
-
Replaces every occurrence of the string
sub
ins
with the stringby
.See also:
- find func
- replace func for replacing single characters
- replaceWord func
- multiReplace func
-
func replaceWord(s, sub: string; by = ""): string {....gcsafe, extern: "nsuReplaceWord", raises: [], tags: [], forbids: [].}
-
Replaces every occurrence of the string
sub
ins
with the stringby
.Each occurrence of
Source Editsub
has to be surrounded by word boundaries (comparable to\b
in regular expressions), otherwise it is not replaced. -
func rfind(s, sub: string; start: Natural = 0; last = -1): int {....gcsafe, extern: "nsuRFindStr", raises: [], tags: [], forbids: [].}
-
Searches for
sub
ins
inside rangestart..last
(both ends included) included) in reverse -- starting at high indexes and moving lower to the first character orstart
. Iflast
is unspecified, it defaults tos.high
(the last element).Searching is case-sensitive. If
sub
is not ins
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func rfind(s: string; chars: set[char]; start: Natural = 0; last = -1): int {. ...gcsafe, extern: "nsuRFindCharSet", raises: [], tags: [], forbids: [].}
-
Searches for
chars
ins
inside rangestart..last
(both ends included) in reverse -- starting at high indexes and moving lower to the first character orstart
. Iflast
is unspecified, it defaults tos.high
(the last element).If
s
contains none of the characters inchars
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func rfind(s: string; sub: char; start: Natural = 0; last = -1): int {....gcsafe, extern: "nsuRFindChar", raises: [], tags: [], forbids: [].}
-
Searches for
sub
ins
inside rangestart..last
(both ends included) in reverse -- starting at high indexes and moving lower to the first character orstart
. Iflast
is unspecified, it defaults tos.high
(the last element).Searching is case-sensitive. If
sub
is not ins
, -1 is returned. Otherwise the index returned is relative tos[0]
, notstart
. Subtractstart
from the result for astart
-origin index.See also:
Source Edit -
func rsplit(s: string; sep: char; maxsplit: int = -1): seq[string] {....gcsafe, extern: "nsuRSplitChar", raises: [], tags: [], forbids: [].}
-
The same as the rsplit iterator, but is a func that returns a sequence of substrings.
A possible common use case for
rsplit
is path manipulation, particularly on systems that don't use a common delimiter.For example, if a system had
#
as a delimiter, you could do the following to get the tail of the path:var tailSplit = rsplit("Root#Object#Method#Index", '#', maxsplit=1)
Results in
tailSplit
containing:@["Root#Object#Method", "Index"]
See also:
Source Edit -
func rsplit(s: string; sep: string; maxsplit: int = -1): seq[string] {....gcsafe, extern: "nsuRSplitString", raises: [], tags: [], forbids: [].}
-
The same as the rsplit iterator, but is a func that returns a sequence of substrings.
A possible common use case for
rsplit
is path manipulation, particularly on systems that don't use a common delimiter.For example, if a system had
#
as a delimiter, you could do the following to get the tail of the path:var tailSplit = rsplit("Root#Object#Method#Index", "#", maxsplit=1)
Results in
tailSplit
containing:@["Root#Object#Method", "Index"]
Note: Empty separator string results in returning an original string, following the interpretation "split by no element".See also:
Example:
Source EditdoAssert "a largely spaced sentence".rsplit(" ", maxsplit = 1) == @[ "a largely spaced", "sentence"] doAssert "a,b,c".rsplit(",") == @["a", "b", "c"] doAssert "a man a plan a canal panama".rsplit("a ") == @["", "man ", "plan ", "canal panama"] doAssert "".rsplit("Elon Musk") == @[""] doAssert "a largely spaced sentence".rsplit(" ") == @["a", "", "largely", "", "", "", "spaced", "sentence"] doAssert "empty sep returns unsplit s".rsplit("") == @["empty sep returns unsplit s"]
-
func rsplit(s: string; seps: set[char] = Whitespace; maxsplit: int = -1): seq[ string] {....gcsafe, extern: "nsuRSplitCharSet", raises: [], tags: [], forbids: [].}
-
The same as the rsplit iterator, but is a func that returns a sequence of substrings.
A possible common use case for
rsplit
is path manipulation, particularly on systems that don't use a common delimiter.For example, if a system had
#
as a delimiter, you could do the following to get the tail of the path:var tailSplit = rsplit("Root#Object#Method#Index", {'#'}, maxsplit=1)
Results in
tailSplit
containing:@["Root#Object#Method", "Index"]
Note: Empty separator set results in returning an original string, following the interpretation "split by no element".See also:
Source Edit -
func spaces(n: Natural): string {.inline, ...raises: [], tags: [], forbids: [].}
-
Returns a string with
n
space characters. You can use this func to left align strings.See also:
Example:
Source Editlet width = 15 text1 = "Hello user!" text2 = "This is a very long string" doAssert text1 & spaces(max(0, width - text1.len)) & "|" == "Hello user! |" doAssert text2 & spaces(max(0, width - text2.len)) & "|" == "This is a very long string|"
-
func split(s: string; sep: char; maxsplit: int = -1): seq[string] {....gcsafe, extern: "nsuSplitChar", raises: [], tags: [], forbids: [].}
-
The same as the split iterator (see its documentation), but is a func that returns a sequence of substrings.
See also:
Example:
Source EditdoAssert "a,b,c".split(',') == @["a", "b", "c"] doAssert "".split(' ') == @[""]
-
func split(s: string; sep: string; maxsplit: int = -1): seq[string] {....gcsafe, extern: "nsuSplitString", raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings using a string separator.Substrings are separated by the string
sep
. This is a wrapper around the split iterator.Note: Empty separator string results in returning an original string, following the interpretation "split by no element".See also:
Example:
Source EditdoAssert "a,b,c".split(",") == @["a", "b", "c"] doAssert "a man a plan a canal panama".split("a ") == @["", "man ", "plan ", "canal panama"] doAssert "".split("Elon Musk") == @[""] doAssert "a largely spaced sentence".split(" ") == @["a", "", "largely", "", "", "", "spaced", "sentence"] doAssert "a largely spaced sentence".split(" ", maxsplit = 1) == @["a", " largely spaced sentence"] doAssert "empty sep returns unsplit s".split("") == @["empty sep returns unsplit s"]
-
func split(s: string; seps: set[char] = Whitespace; maxsplit: int = -1): seq[ string] {....gcsafe, extern: "nsuSplitCharSet", raises: [], tags: [], forbids: [].}
-
The same as the split iterator (see its documentation), but is a func that returns a sequence of substrings.
Note: Empty separator set results in returning an original string, following the interpretation "split by no element".
See also:
Example:
Source EditdoAssert "a,b;c".split({',', ';'}) == @["a", "b", "c"] doAssert "".split({' '}) == @[""] doAssert "empty seps return unsplit s".split({}) == @["empty seps return unsplit s"]
-
func splitLines(s: string; keepEol = false): seq[string] {....gcsafe, extern: "nsuSplitLines", raises: [], tags: [], forbids: [].}
-
The same as the splitLines iterator (see its documentation), but is a func that returns a sequence of substrings.
See also:
Source Edit -
func splitWhitespace(s: string; maxsplit: int = -1): seq[string] {....gcsafe, extern: "nsuSplitWhitespace", raises: [], tags: [], forbids: [].}
-
The same as the splitWhitespace iterator (see its documentation), but is a func that returns a sequence of substrings.
See also:
Source Edit -
func startsWith(s, prefix: string): bool {....gcsafe, extern: "nsuStartsWith", raises: [], tags: [], forbids: [].}
-
Returns true if
s
starts with stringprefix
.If
prefix == ""
true is returned.See also:
Example:
Source Editlet a = "abracadabra" doAssert a.startsWith("abra") == true doAssert a.startsWith("bra") == false
-
func strip(s: string; leading = true; trailing = true; chars: set[char] = Whitespace): string {....gcsafe, extern: "nsuStrip", raises: [], tags: [], forbids: [].}
-
Strips leading or trailing
chars
(default: whitespace characters) froms
and returns the resulting string.If
leading
is true (default), leadingchars
are stripped. Iftrailing
is true (default), trailingchars
are stripped. If both are false, the string is returned unchanged.See also:
- strip proc Inplace version.
- stripLineEnd func
Example:
Source Editlet a = " vhellov " let b = strip(a) doAssert b == "vhellov" doAssert a.strip(leading = false) == " vhellov" doAssert a.strip(trailing = false) == "vhellov " doAssert b.strip(chars = {'v'}) == "hello" doAssert b.strip(leading = false, chars = {'v'}) == "vhello" let c = "blaXbla" doAssert c.strip(chars = {'b', 'a'}) == "laXbl" doAssert c.strip(chars = {'b', 'a', 'l'}) == "X"
-
func stripLineEnd(s: var string) {....raises: [], tags: [], forbids: [].}
-
Strips one of these suffixes from
s
in-place:\r, \n, \r\n, \f, \v
(at most once instance). For example, can be useful in conjunction withosproc.execCmdEx
. aka: chompExample:
Source Editvar s = "foo\n\n" s.stripLineEnd doAssert s == "foo\n" s = "foo\r\n" s.stripLineEnd doAssert s == "foo"
-
func toBin(x: BiggestInt; len: Positive): string {....gcsafe, extern: "nsuToBin", raises: [], tags: [], forbids: [].}
-
Converts
x
into its binary representation.The resulting string is always
len
characters long. No leading0b
prefix is generated.Example:
Source Editlet a = 29 b = 257 doAssert a.toBin(8) == "00011101" doAssert b.toBin(8) == "00000001" doAssert b.toBin(9) == "100000001"
-
func toHex(s: string): string {....gcsafe, raises: [], tags: [], forbids: [].}
-
Converts a bytes string to its hexadecimal representation.
The output is twice the input long. No prefix like
0x
is generated.See also:
- parseHexStr func for the reverse operation
Example:
Source Editlet a = "1" b = "A" c = "\0\255" doAssert a.toHex() == "31" doAssert b.toHex() == "41" doAssert c.toHex() == "00FF"
-
func toHex[T: SomeInteger](x: T): string
-
Shortcut for
toHex(x, T.sizeof * 2)
Example:
Source EditdoAssert toHex(1984'i64) == "00000000000007C0" doAssert toHex(1984'i16) == "07C0"
-
func toHex[T: SomeInteger](x: T; len: Positive): string
-
Converts
x
to its hexadecimal representation.The resulting string will be exactly
len
characters long. No prefix like0x
is generated.x
is treated as an unsigned value.Example:
Source Editlet a = 62'u64 b = 4097'u64 doAssert a.toHex(3) == "03E" doAssert b.toHex(3) == "001" doAssert b.toHex(4) == "1001" doAssert toHex(62, 3) == "03E" doAssert toHex(-8, 6) == "FFFFF8"
-
func toLowerAscii(c: char): char {....gcsafe, extern: "nsuToLowerAsciiChar", raises: [], tags: [], forbids: [].}
-
Returns the lower case version of character
c
.This works only for the letters
A-Z
. See unicode.toLower for a version that works for any Unicode character.See also:
- isLowerAscii func
- toLowerAscii func for converting a string
Example:
Source EditdoAssert toLowerAscii('A') == 'a' doAssert toLowerAscii('e') == 'e'
-
func toLowerAscii(s: string): string {....gcsafe, extern: "nsuToLowerAsciiStr", raises: [], tags: [], forbids: [].}
-
Converts string
s
into lower case.This works only for the letters
A-Z
. See unicode.toLower for a version that works for any Unicode character.See also:
Example:
Source EditdoAssert toLowerAscii("FooBar!") == "foobar!"
-
func toOct(x: BiggestInt; len: Positive): string {....gcsafe, extern: "nsuToOct", raises: [], tags: [], forbids: [].}
-
Converts
x
into its octal representation.The resulting string is always
len
characters long. No leading0o
prefix is generated.Do not confuse it with toOctal func.
Example:
Source Editlet a = 62 b = 513 doAssert a.toOct(3) == "076" doAssert b.toOct(3) == "001" doAssert b.toOct(5) == "01001"
-
func toOctal(c: char): string {....gcsafe, extern: "nsuToOctal", raises: [], tags: [], forbids: [].}
-
Converts a character
c
to its octal representation.The resulting string may not have a leading zero. Its length is always exactly 3.
Do not confuse it with toOct func.
Example:
Source EditdoAssert toOctal('1') == "061" doAssert toOctal('A') == "101" doAssert toOctal('a') == "141" doAssert toOctal('!') == "041"
-
func toUpperAscii(c: char): char {....gcsafe, extern: "nsuToUpperAsciiChar", raises: [], tags: [], forbids: [].}
-
Converts character
c
into upper case.This works only for the letters
A-Z
. See unicode.toUpper for a version that works for any Unicode character.See also:
- isUpperAscii func
- toUpperAscii func for converting a string
- capitalizeAscii func
Example:
Source EditdoAssert toUpperAscii('a') == 'A' doAssert toUpperAscii('E') == 'E'
-
func toUpperAscii(s: string): string {....gcsafe, extern: "nsuToUpperAsciiStr", raises: [], tags: [], forbids: [].}
-
Converts string
s
into upper case.This works only for the letters
A-Z
. See unicode.toUpper for a version that works for any Unicode character.See also:
Example:
Source EditdoAssert toUpperAscii("FooBar!") == "FOOBAR!"
-
func trimZeros(x: var string; decimalSep = '.') {....raises: [], tags: [], forbids: [].}
-
Trim trailing zeros from a formatted floating point value
x
(must be declared asvar
).This modifies
x
itself, it does not return a copy.Example:
Source Editvar x = "123.456000000" x.trimZeros() doAssert x == "123.456"
-
func unescape(s: string; prefix = "\""; suffix = "\""): string {....gcsafe, extern: "nsuUnescape", raises: [ValueError], tags: [], forbids: [].}
-
Unescapes a string
s
.This complements escape func as it performs the opposite operations.
If
Source Edits
does not begin withprefix
and end withsuffix
a ValueError exception will be raised. -
func unindent(s: string; count: Natural = int.high; padding: string = " "): string {. ...gcsafe, extern: "nsuUnindent", raises: [], tags: [], forbids: [].}
-
Unindents each line in
s
bycount
amount ofpadding
.Note: This does not preserve the new line characters used in
s
.See also:
Example:
Source Editlet x = """ Hello There """.unindent() doAssert x == "Hello\nThere\n"
-
func validIdentifier(s: string): bool {....gcsafe, extern: "nsuValidIdentifier", raises: [], tags: [], forbids: [].}
-
Returns true if
s
is a valid identifier.A valid identifier starts with a character of the set
IdentStartChars
and is followed by any number of characters of the setIdentChars
.Example:
Source EditdoAssert "abc_def08".validIdentifier
Iterators
-
iterator rsplit(s: string; sep: char; maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings from the right using a string separator. Works exactly the same as split iterator except in reverse order.for piece in "foo:bar".rsplit(':'): echo piece
Results in:
"bar" "foo"
Substrings are separated from the right by the char
sep
.See also:
Source Edit -
iterator rsplit(s: string; sep: string; maxsplit: int = -1; keepSeparators: bool = false): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings from the right using a string separator. Works exactly the same as split iterator except in reverse order.for piece in "foothebar".rsplit("the"): echo piece
Results in:
"bar" "foo"
Substrings are separated from the right by the string
sep
Note: Empty separator string results in returning an original string, following the interpretation "split by no element".See also:
Source Edit -
iterator rsplit(s: string; seps: set[char] = Whitespace; maxsplit: int = -1): string {. ...raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings from the right using a string separator. Works exactly the same as split iterator except in reverse order.for piece in "foo bar".rsplit(WhiteSpace): echo piece
Results in:
"bar" "foo"
Substrings are separated from the right by the set of chars
seps
Note: Empty separator set results in returning an original string, following the interpretation "split by no element".See also:
Source Edit -
iterator split(s: string; sep: char; maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings using a single separator.Substrings are separated by the character
sep
. The code:for word in split(";;this;is;an;;example;;;", ';'): writeLine(stdout, word)
Results in:
"" "" "this" "is" "an" "" "example" "" "" ""
See also:
Source Edit -
iterator split(s: string; sep: string; maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings using a string separator.Substrings are separated by the string
sep
. The code:for word in split("thisDATAisDATAcorrupted", "DATA"): writeLine(stdout, word)
Results in:
"this" "is" "corrupted"
Note: Empty separator string results in returning an original string, following the interpretation "split by no element".See also:
Source Edit -
iterator split(s: string; seps: set[char] = Whitespace; maxsplit: int = -1): string {. ...raises: [], tags: [], forbids: [].}
-
Splits the string
s
into substrings using a group of separators.Substrings are separated by a substring containing only
seps
.for word in split("this\lis an\texample"): writeLine(stdout, word)
...generates this output:
"this" "is" "an" "example"
And the following code:
for word in split("this:is;an$example", {';', ':', '$'}): writeLine(stdout, word)
...produces the same output as the first example. The code:
let date = "2012-11-20T22:08:08.398990" let separators = {' ', '-', ':', 'T'} for number in split(date, separators): writeLine(stdout, number)
...results in:
"2012" "11" "20" "22" "08" "08.398990"
Note: Empty separator set results in returning an original string, following the interpretation "split by no element".See also:
Source Edit -
iterator splitLines(s: string; keepEol = false): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
into its containing lines.Every character literal newline combination (CR, LF, CR-LF) is supported. The result strings contain no trailing end of line characters unless the parameter
keepEol
is set totrue
.Example:
for line in splitLines("\nthis\nis\nan\n\nexample\n"): writeLine(stdout, line)
Results in:
"" "this" "is" "an" "" "example" ""
See also:
Source Edit -
iterator splitWhitespace(s: string; maxsplit: int = -1): string {....raises: [], tags: [], forbids: [].}
-
Splits the string
s
at whitespace stripping leading and trailing whitespace if necessary. Ifmaxsplit
is specified and is positive, no more thanmaxsplit
splits is made.The following code:
let s = " foo \t bar baz " for ms in [-1, 1, 2, 3]: echo "------ maxsplit = ", ms, ":" for item in s.splitWhitespace(maxsplit=ms): echo '"', item, '"'
...results in:
------ maxsplit = -1: "foo" "bar" "baz" ------ maxsplit = 1: "foo" "bar baz " ------ maxsplit = 2: "foo" "bar" "baz " ------ maxsplit = 3: "foo" "bar" "baz"
See also:
Source Edit -
iterator tokenize(s: string; seps: set[char] = Whitespace): tuple[token: string, isSep: bool] {....raises: [], tags: [], forbids: [].}
-
Tokenizes the string
s
into substrings.Substrings are separated by a substring containing only
seps
. Example:for word in tokenize(" this is an example "): writeLine(stdout, word)
Results in:
Source Edit(" ", true) ("this", false) (" ", true) ("is", false) (" ", true) ("an", false) (" ", true) ("example", false) (" ", true)
Exports
© 2006–2024 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/strutils.html