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

Image for post
Image for post
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:

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.

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”.

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

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

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

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.

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

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

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

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.

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

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.

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