Solution: Print Part of a String

This lesson provides a solution to the challenge: print part of a string.

We'll cover the following...

Solution #

Here is the code that will print the part between the first “e” and the last “e” letter in a line.

Press + to interact
import std.stdio;
import std.string;
void PrintPart() {
string line = "this line has five words";
ptrdiff_t first_e = indexOf(line, 'e');
if (first_e == -1) {
writeln("There is no letter e in this line.");
} else {
ptrdiff_t last_e = lastIndexOf(line, 'e');
writeln(line[first_e .. last_e + 1]);
}
}

Solution explanation:

...