Lisp - String Case Conversion



Lisp provides several methods for string case conversion.

Constructive Conversion

Following methods convert the string case and returned a new string as result.

Sr.No. Function & Description
1

string-upcase

Converts the string to upper case

2

string-downcase

Converts the string to lower case

3

string-capitalize

Capitalizes each word in the string

Destructive Conversion

Following methods convert the string case and modify the existing string.

Sr.No. Function & Description
1

nstring-upcase

Converts the string to upper case

2

nstring-downcase

Converts the string to lower case

3

nstring-capitalize

Capitalizes each word in the string

Example - Using Constructive Conversion

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

main.lisp

; convert to lower case and print statement
(write-line (string-downcase "a big HELLO from tutorials Point"))
; convert to upper case and print statement
(write-line (string-upcase "a big hello from tutorials point"))
; convert to Capital case and print statement
(write-line (string-capitalize "a big hello from tutorials point"))

Output

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

a big hello from tutorials point
A BIG HELLO FROM TUTORIALS POINT
A Big Hello From Tutorials Point

Example - Using Destructive Conversion

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

main.lisp

(setq str "hello world")
(nstring-upcase str) 

Output

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

HELLO WORLD

Using Start/End Indices

We can use start and end indices as well while converting strings to convert part of string.

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

main.lisp

(write-line (string-upcase "a big hello from tutorials point"  :start 6))

Output

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

a big HELLO FROM TUTORIALS POINT
Advertisements