Learn Programming

Learn D Programming Lesson 4 - More on strings

In the previous lesson we learnt a bit about strings. This lesson is a continuation on from what was learnt in that lesson. This lesson has a little bit of information about escape sequences and then we look at how to perform some common tasks with strings. All of the string commands in this lesson require that you import the std.string module at the top of your program before you can use them.

Escape sequences

An escape sequence is something you put in a string in situations where putting a normal character in the string would make the compiler interpret that character in the wrong way. An example of this is when you want to put double quotes in a string. The problem is that the double quotes will make the compiler think that you are closing the string contents. To get around it you must put the double quotes escape sequence in the string. Escape sequences all start with a backslash(\) and then have a character after them to say what they are for. Here is an example of how to escape double quotes in a string which shows how the double quotes escape sequence (/") is used to put double quotes around the words double quotes.

string MyString = "Here are some \"double quotes\"";
writefln(MyString);

Sometimes escape sequences use letters instead of symbols. An example is the new line character. It is a backslash followed by the letter n. Here is an example that shows how to add 3 new lines at the end of a string. This example uses writef instead of writefln because it makes it easier to see the effect of the escape sequences.

string MyString = "Some new lines\n\n\n";
writef(MyString);

Here are some of the most often used escape sequences.

\'Single quote
\"Double quotes
\\Backslash
\nNew line
\rCarriage return (Similar to new line)
\r\nCarriage return and line feed (Often used in Windows)
\tTab

Joining strings

To join 2 strings together you must use the string joining operator which is a tilde(~). Here is an example that shows how to join some strings together.

string string1 = "abc";
string string2 = string1 ~ "def";
writefln(string2);

Getting the length of a string

If you want to get the number of characters in a string then you can use the length property of the string. To use it you need to add a dot after the name of the string and then the word length. It is best explained with an example.

string MyString = "This is a string";
int StringLength = MyString.length;
writefln(StringLength);

Accessing individual characters of a string

You can get a single character from anywhere in a string by putting the number of the position of the character in square brackets after the name of the string. There is a catch however which is that the position numbers start at 0 instead of 1 so if you want the character at position 4 for example then you have to use the number 3. Here is an example that shows how to get the letter s from the word This.

string MyString = "This is a string";
char MyChar = MyString[3];
writefln(MyChar);

Getting part of a string

You can get a certain part of a string using square brackets behind the name of the string. There are 3 things that must go in the square brackets. The first is the position to start copying the string from. Then put 2 dots after that. The third thing is the number of characters to copy from that position. The position starts counting from 0 but the number of characters starts counting from 1 as usual which makes it a little bit confusing so be careful. Here is an example of how to copy the word This from a string.

string MyString = "This is a string";
string FirstWord = MyString[0..4];
writefln(FirstWord);

Changing a string's case

You can change a string so that all its letters are in uppercase using the toupper command and you can change all the letters to lowercase using the tolower command. Here is an example of how to use both of them.

string MyString = "This is a string";
string UpperString = toupper(MyString);
string LowerString = tolower(MyString);
writefln(UpperString);
writefln(LowerString);

Testing if a string is a number

You can find out whether a string contains a number using the isNumeric command. This is useful for when you get the user to enter a number because all input from the user comes in as a string and has to be converted to an integer. The isNumeric command returns a bool which means that it will return true if the string is a number and false if it is not. Here is an example that checks if a string is a number and then prints out the result.

string MyString = "123";
bool IsANum = isNumeric(MyString);
writefln(IsANum);

Practice

Try write your own programs to use the commands you have learnt in different ways. For example try writing a program that asks the user for their first, middle and last names and then prints the user's name out again but with their initials instead of the full name. Also look at these string handling functions and see if you can figure out how to use some of them.