Part 5 — A complete beginner’s guide to Computer Programming with Clojure: Text Processing.

Photo by Tara Evans on Unsplash

In programming, we refer to text as a string, as in a string of characters. In this Part, we will discuss string processing, which includes manipulation, formatting, and obtaining information about a string.

Fire up a terminal and begin a new REPL session. Type in the following statements, and observe the results:

(def ourString “Close Encounters of the 3rd Kind”)

The keyword def allows us to create a static variable called ourString and assign it with the string, “Close Encounters of the 3rd Kind”. Creating static variables is useful in programming for many reasons, but mostly because it saves on typing! You may have notices that the static variable begins with a lowercase character with an upper case character in the middle, ourString. This is a common naming convention known as Camel Case.

Continue to type the following statements shown in bold.

(clojure.string/join “ “ [“Star” “Trek”])

This simply joins two separate strings held in a Vector and places a space between each string to produce a single string thus, “Star Trek”.

(clojure.string/join [“Star” “Trek”])

Similar to the previous example, only now we have omitted the space. Now we get, “StarTrek”.

(clojure.string/replace ourString #”3rd” “4th”)

This should be fairly obvious. We will replace 3rd with 4th.

(clojure.string/upper-case ourString)

Convert all the characters in the string to upper-case.

(clojure.string/lower-case ourString)

Convert all the characters in the string to lower-case.

The screen capture below shows all the examples above. This screen capture is just a reference to ensure you got the same results.

This time we will look at String formatting. We will add a couple more static variables. Each will hold a specific number.

(def ourString “Close Encounters of the 3rd Kind”)

As before, we will use the same static variable and assign it the same string value.

(def doubleNo 16.534)(def longNo 453)

These are our two static variables, doubleNo and longNo. Each one holds a different number.

(format “SciFi Film — %s” ourString)

This demonstrates the use of static variables. The %s will be replaced by the string contained in ourString.

(format “10 spaces and %10d” longNo)(format “20 spaces and %20d” longNo)

The two statements above demonstrate how to add spaces as a formatting option. In addition, the new string also appends the number stored in longNo.

(format “Five Leading zero %08d” longNo)(format “One Leading zero %04d” longNo)

These two appear confusing at first. You have %08d to produce five leading 0’s, and %04d to produce just one 0. However, this formatting takes into account the number of digits stored in longNo i.e. 453. So, 0453 is 4 digits and 00000453 is 8 digits in total.

Continue to type the following and observe the results

(format “%-4d Left Justified” longNo)(format “%-20d Left Justified” longNo)(format “Right Justified %4d” longNo)(format “Right Justified %20d” longNo)(format “Show as five decimal places %.5f” doubleNo)(format “Show as two decimal places %.2f” doubleNo)

The screen capture below shows all the examples above. As before, this is just a reference to ensure you got the same results.

In Part 4, we wrote a program (Listing 1.) to find the square root of a number. I later explained how this code could act as a template or boilerplate for future coding. I also further explained, it had all the basic elements of a Program: It took in Input and Processed it to provide the result as Output.

Listing 2. below uses the same format. Only this time, the Code (Program) with take in a String as Input and convert it (Processing) to Upper-case and display the result (Output).

Listing 2.

(import ‘(javax.swing JFrame JLabel JTextField JButton)‘(java.awt.event ActionListener)‘(java.awt GridLayout))((let [frame (JFrame. “String Capitilizer!”)input-text (JTextField.)inputNo-label (JLabel. “← Type in a string”)convert-button (JButton. “CONVERT”)result-label (JLabel. “converted text will appear here”)](. convert-button(addActionListener(proxy [ActionListener] [ ](actionPerformed [evt](let [i (. input-text (getText))](. result-label(setText (str (clojure.string/upper-case i) “ — totally converted!!”))))))))(doto frame(.setLayout (new GridLayout 2 2 5 5))(.add input-text)(.add inputNo-label)(.add convert-button)(.add result-label)(.setSize 900 100)(.setVisible true))))

Here is our String conversion App

And after process our Input

SUMMARY

Text manipulation is one of the staple skills of Computer Programming. Later in this course, we will look at techniques for extracting specific text or patterns. Hopefully, you appreciate the versatility of our boilerplate app code? Try and re-write the code to convert into lower-case. Also, practice writing your own code snippets to produce different formating and string manipulation results.

Finally, consider how the App could be modified to contain more than one button? For instance, one button for upper-case and another for lower-case within the same App.

Previous

Part 6 — Lists, Sets, Vectors, and Maps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store