This tutorials talks about how to store characters and strings in Nim Language.

Nim Provides following data types

  • Nim Character

  • String

  • NIm Character

Characters is a ascii digit represented by Char type in Nim and size is 1 byte in length.

It represents single character such as digit,letters, symbol, and special characters, and throws an error if it contains multiple characters These are enclosed in single tick mark (`)

Example character variable

let 
    char1=`a`
    char2=`b`

Invalid Character declaration and throws an error

let 
    char4=`23`
    char5=`22`

Char type does not represent UTF-8 characters. use Rune type for represent unicode characters. Rune is declared unicode module which needs import

String in NIM

String are group of characters, that contains words or statements or escape characters declared using double quotes

  • String literals with escape character
  • Raw String literals
  • Long String literals

Example of String literals

let
    str="abc"
    str1="2"

Multi line strings are declared using \n character

let
    str="abc\n def"

Another, Raw String literals, which does not escape characters print the string without escape characters. Raw strings starts with r character and enclose string in double quotes.

echo r"Skip escape characters such as \n and \t"

Output:

Skip escape characters such as \n and \t

Long string literals:

Sometimes, We want to add long line string, enclosed in triple quotes (""").

Long string literals are spanned across multiple lines and \ is not escape character

Example:

echo “““First line. Second line third line four line \n \t \r”””

String Formatting example

strutils module provides formatting feature

String concatenation example

Sometimes, adding one string to another string.

& operator used to append a string.

let str = "one" & " two"
echo str # one two

How to check strings are equal or not

There are two operators to check strings are equal or not

  • ==: check if two strings are equal and return true.
  • !==: check if two strings are not equal and return true.
echo "one" == "two"
echo "one" != "two"