Working with strings

Prerequisites

Before starting this lesson, you should be familiar with:

Learning Objectives

After completing this lesson, learners should be able to:
  • Construct complex strings, e.g. to produce log messages and create file paths

  • Master the backward slash .

Motivation

Combining several strings into a larger string is a prevalent operation in scripting. This is useful, e.g., to create file paths and create log messages. Such concatenation of strings is surprisingly error prone and it is thus important to learn it properly and be aware of all the pitfalls.

Concept map

graph TD S("Strings") --> L("Log/debug messages") S --> F("File paths") S --> O("Script output (tables)")



Figure


Examples of string expressions as they may appear in a typical bioimage analysis script (in some hypothetical scripting language).



Activities


Show activity for:  

ImageJ Macro

// log message
i = 11;
n = 100;
print("Analyzing image "+i+"/"+n+"...");

// paths
tmpFolder = getDirectory("temp");
fileName = "nuclei.tif";
path = tmpFolder + File.separator + fileName;
print(path);

// escape character and special characters
print("\\"); // print("\"); will throw an error

print("\"\\\"");

print("Line 1.\nLine 2.");

print("1\t2\t3");

Exercises

Show exercise/solution for:

ImageJ Macro: Concatenate variables

Open a new script editor

  1. From ImageJ GUI open a script [ File > New > Script…]
  2. Choose the language IJ1 Macro
  3. Define a string variable with the content "nucleus"
  4. Define a numerical variable with the content 6
  5. Concatenate the variables to get the final string "nucleus_6"
  6. Print the final string

Solution

// define variables
str = "nucleus";
num = 6;
// concatenate
concat = str + "_" + num;
// print the string to the log window
print(concat);

ImageJ Macro: Create function arguments

Create a macro that applies a gaussian blur with a certain sigma value to an image.

  1. Open a sample image: [ File > Open samples > Blobs ]
  2. Open the script editor: [ File > New > Script…]
  3. Choose the language IJ1 Macro
  4. Add the line run("Gaussian Blur...", "sigma=6");
    • Tip: In programming copy and paste as much as possible!
  5. Run the macro to see the effect on the image
  6. Define a variable blurSigma with the value 6
  7. Replace the 6 in the run(...) command with this variable.

Solution

blurSigma = 6;
run("Gaussian Blur...", "sigma="+blurSigma );
// Below would also work and is very convenient, but a bit non-standard...
// run("Gaussian Blur...", "sigma=&blurSigma");

ImageJ Macro: Create paths

Concatenatimg folder and file names

  1. [ File › Open Samples › Blobs ]
  2. Save the image onto the Desktop of your computer
  3. Open the script editor: [ File > New > Script…]
  4. Choose the language IJ1 Macro
  5. Define folder as a string variable pointing to your computer’s desktop folder (it should be something like C:\Users\Username\Desktop or /Users/Username/Deskop)
  6. If you are on Windows, watch out: In order to have C:\Users\Username\Desktop you will have to write C:\\Users\\Username\\Desktop, because \ is a special character that needs to be “escaped”
  7. Define fileName as a string representing the file name of the image that you just saved (it should be something like "blobs.tiff")
  8. Concatenate the variables to get a new string (filePath) representing the full file path (folder + File.Separator + fileName).
  9. Use the filePath variable to add code that prints something like “Opening C:\Users\Username\blobs.tiff”
  10. Add open(filePath); to open the image.
  11. Run the macro

Solution

// define the variables
folder = "/Users/Username/Desktop/"; // <= This must be replaced!
fileName = "blobs.tiff"; // <= Be careful: There is ".tiff" and ".tif"
// concatenating, adding the file separator in the middle
filePath = folder + File.separator + fileName;
// print log message
print("Opening " + filePath);
// open the file
open(filePath);



Assessment

Fill in the blanks

  1. In MacOS and Linux sub-folders are separated by the ___ string, whereas on Windows they are separated by the ___ string.
  2. Concatenating "Hello" and "world" one obtains ___.
  3. Concatenation the variables folder = "/Users/Images" and file = "MyImage.tif" one obtains ___.

Solution

  1. MacOs "/", Windows "\"
  2. One would get "Helloworld"; to fix this one needs to add a third " " string in the middle to get "Hello world".
  3. One would obetain "/Users/ImagesMyImage.tif". There are several ways to fix this, depending on the scripting language. A good way is to use functions such as, e.g., os.path.join( folder, file ) in python, because this will work for both cases: folder = "/Users/Images" and folder = "/Users/Images/".

Explanations

Creating paths

A frequent operation in bioimage analysis is to create paths to images by concatenating a folder and file name to a full path. Please note that when concatenating a folder and a file name into a full path, you might need to add a so-called file separator between the folder and the file name. This is a character that separates directory names within a path to a particular location on your computer. Different operating systems use different file separators: on Linux and MacOS, this is /, while Windows uses \. To make it worse, when you store a directory you are typically never sure whether the contained string ends on / or \ or does not have the separator in the end, e.g. C:\Users\Data, in which case you have to add it when concatenating a file name). To make it even worse, in some programming langauges the \ character have a special meaning within strings and is thus not simply interpreted as a character and to actually get a backslash you may have to write \\.

If you want to have some “fun” you can read those discussions:

As all of this can quickly become a huge mess, fortunately, scripting languages typically offer special functions to help you write code to create file paths that will run on all operating systems.

String concatenation

String concatenation is the operation of joining multiple substrings.

For example concatenating “Hello “ and “world!” would result into “Hello world!”.




Follow-up material

Recommended follow-up modules:

Learn more: