Strings#
A string
is a data type that can store any text you might want. We have already seen strings before, for example "Hello World", and we have stored strings as variables before, for instance when we stored the string
"This is also a variable" in the variable message
:
The ability to store and manipulate strings is a key part of ImageJ Macro
programming, so let's take a close look.
We can create a string in the same way we create any other variable, by picking a name and assigning a value to it. In the case of strings, the value must be enclosed in quotation marks "
.
We can combine two strings using the +
operator, for example:
- Remember that we need a space either at the end of the first string or at the beginning of the second string, or else the concatenation will be "This isa String"
We can also include other variables in a string, for example:
Strings can be split at various points. This is very useful for, as an example, removing file extensions (such as .tif
from file names).
If we had the following string: myfile.tif
and wanted to get only the file name, we could split
the string at the .
.
string = "myfile.tif";
split_string = split(string, '.');
print(split_string[0] + ", " + split_string[1]);
What is stored in the variable split_string
? Hint: Look at the syntax for printing the returned value
The split
function returns an array which contains the split strings. In this case, the array would contain the values myfile
and tif
(because we have chosen to split the string at the .
).