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
Figure
Activities
- Open a script editor.
- Create a log message
- Define two variables:
i
with value11
andn
with value100
. - Using those two variables create the message:
"Analyzing image 11/100..."
.
- Define two variables:
- Create a file path
- Use an in-built function to print the string that separates folders on your operating system
- Get the temp folder on your system and store it in a variable
- Create a path to a hypothetical file in the temp folder with the help of the separator string
- Explore the escape string
\
- Print
\
, depending on the language that may be a little challenge. - Print the string
"\"
(it should actually print the quotation marks!) - Create a string with two sentences and a line break.
- Print tab separated values.
- Print
Show activity for:
ImageJ Macro
Exercises
Show exercise/solution for:ImageJ Macro: Concatenate variables
Open a new script editor
- From ImageJ GUI open a script [ File > New > Script…]
- Choose the language IJ1 Macro
- Define a string variable with the content
"nucleus"
- Define a numerical variable with the content
6
- Concatenate the variables to get the final string
"nucleus_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.
- Open a sample image: [ File > Open samples > Blobs ]
- Open the script editor: [ File > New > Script…]
- Choose the language IJ1 Macro
- Add the line
run("Gaussian Blur...", "sigma=6");
- Tip: In programming copy and paste as much as possible!
- Run the macro to see the effect on the image
- Define a variable
blurSigma
with the value6
- Replace the
6
in therun(...)
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
- [ File › Open Samples › Blobs ]
- Save the image onto the Desktop of your computer
- Open the script editor: [ File > New > Script…]
- Choose the language IJ1 Macro
- Define
folder
as a string variable pointing to your computer’s desktop folder (it should be something likeC:\Users\Username\Desktop
or/Users/Username/Deskop
)- If you are on Windows, watch out: In order to have
C:\Users\Username\Desktop
you will have to writeC:\\Users\\Username\\Desktop
, because\
is a special character that needs to be “escaped”- Define
fileName
as a string representing the file name of the image that you just saved (it should be something like"blobs.tiff"
)- Concatenate the variables to get a new string (
filePath
) representing the full file path (folder + File.Separator + fileName
).- Use the filePath variable to add code that prints something like “Opening C:\Users\Username\blobs.tiff”
- Add
open(filePath);
to open the image.- 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
- In MacOS and Linux sub-folders are separated by the
___
string, whereas on Windows they are separated by the___
string. - Concatenating
"Hello"
and"world"
one obtains___
. - Concatenation the variables
folder = "/Users/Images"
andfile = "MyImage.tif"
one obtains___
.
Solution
- MacOs
"/"
, Windows"\"
- One would get
"Helloworld"
; to fix this one needs to add a third" "
string in the middle to get"Hello world"
.- 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"
andfolder = "/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: