Lisp - String Length



In Lisp, we can get string length using length function.

length function is a versatile function. It can work with multiple types of sequences. As string is one of the type of a sequence, length works perfectly with strings. In this chapter, we'll check multiple scenarios while using length function.

Example - Getting length of a String

Let's define a string and use length function to get length of the string.

Create a new source code file named main.lisp and type the following code in it.

main.lisp

; get length and print length of string
(write (length "Welcome to Tutorialspoint.com"))

Output

When you execute the code, it returns the following result −

29

Example - Getting length of a String as parameter

Let's define a string as a parameter and use length function to get length of the string.

Create a new source code file named main.lisp and type the following code in it.

main.lisp

(defparameter test-string "Welcome to Tutorialspoint.com")
; get length and print length of string
(write (length test-string))

Output

When you execute the code, it returns the following result −

29

Example - Getting length of a back-quoted String

Let's define a back-quoted string and use length function to get length of the string.

Create a new source code file named main.lisp and type the following code in it.

main.lisp

; get length and print length of string
(write (length '"Welcome to Tutorialspoint.com"))

Output

When you execute the code, it returns the following result −

29
Advertisements