Categories: All Variables and Constants  String manipulation  Builtin functions  Input and output  Arrays  Conditional logic  Repetition / Iteration  Functions / Procedures 

Show:

This resource is designed as a quick reference or revision guide. It has not been endorsed by any exam boards. If you spot any mistakes, please let me know and I'll fix them asap.

String manipulation

Any text in a program is stored as a string data type. Strings are made up of characters, which are individual letters, numbers or symbols.

Think of a piece of string hanging like a washing line outside. Pegged to that piece of string is each character in order.

Manipulating strings means changing or working with the characters inside a string.

Top Tips:

  • Concatenating strings means joining strings together
    The string "there" concatenated to the string "hello " becomes the string "hello there"
  • Finding a substring means ignoring part of the string and keeping the part that you want.
    "it" is a substring of "biscuit": it's the 6th and 7th characters (counting from 1).
  • Most programming languages count from 0 rather than 1 when getting a substring
    "it" is a substring of "biscuit": it's the 5th and 6th characters (counting from 0)
Skill
Description
VB.NET
Python
C#
String variables
    

A string is some text such as "Hello" or "Area 51"

Variables let you store data.

String variables let you store text.

In this example a variable called name is set to the string value of "Bob"

# Create a new variable called name name = "Bob"
// Create a new variable called name string name = "Bob";
String constants
    

A string is some text such as "Hello" or "Area 51"

Constants store a value that is set once and then never changes.

String constants let you give some text data a name so that you can use it to make your code easier to read or set options for your code that the user wont be able to change.

In this example, a constant called ERROR_MESSAGE is set to the string value of "Don't panic!"

# Create a new constant called ERROR_MESSAGE ERROR_MESSAGE = "Don't panic!"
// Create a new constant called ERROR_MESSAGE const string ERROR_MESSAGE = "Don't panic!";
Concatenating strings
   

A string is some text such as "Hello"

Concatenating means joining together or adding / appending on to the end.

Concatenating strings means joining two strings together.

In this example, two strings (first_name and last_name) are concatenated (joined together) and saved in another string variable called full_name with a space in between them.

# create three string variables first_name = "Bob" last_name = "Builder" # concatenate first and last names full_name = first_name + " " + last_name
// create three string variables string firstName = "Bob"; string lastName = "Builder"; // concatenate first and last names fullName = firstName + " " + last_name;
Length of a string
    

A string is some text such as "Hello"

The length of a string means how many characters (including spaces, numbers and punctuation) there are in that string

This example createa a string called name and sets it to "Bob". It counts how many characters are in that string and stores the answer (3) into a variable called l

# create string variable name = "Bob" # count characters and store length l = len(name)
// create string variable string name = "Bob"; // count characters and store length int l = name.Length;
Getting a character from a string
   

A string is some text such as "Hello"

A character is a single letter or digit.

Sometimes it's useful to get the 1st, 2nd or whichever character from a string.

This example gets the first character of a string called name and stores it into a variable called first_letter

name = "Bob" # get the first character from name first_letter = name[0]
string name = "Bob"; // get the first character from name char firstLetter = name[0];
Displaying a mixture of text and variables
    

Variables let your program store data of a particular data type.

Integer variables store whole numbers. String variables store text.

Sometimes you need to combine text with integer variables and string variables when you display it to the screen.

This example has a string variable called name (set to "Bob") and an integer variable called score (set to 10). It combines both variables with some text to display the message "Bob has 10 points"

score = 10 name = "Bob" print(name + " has " + str(score) + " points")
int score = 10; string name = "Bob"; Console.WriteLine(name + " has " + score + " points");
Casting (converting) to a string
     

Often it's useful to convert a number (integer or real data type) to text (string data type) so that it can be processed or sent as an output later in the program.

This example converts the integer number 1000 to a string containing "1000"

# create an integer variable int_number = 1000 # cast the integer to a string str_number = str(int_number)
// create an integer variable int intNumber = 1000; // cast the integer to a string string strNumber = intNumber.toString();
Casting (converting) to an integer
     

Often it's useful to round a decimal number (real data type) to a whole number (integer data type).

It can also be useful to convert a string representation of a number (e.g. one typed in by the user) to an integer

This example converts the string "1000" to the integer 1000

# create a string variable containing an integer str_number = "1000" # cast the string to an integer int_number = int(str_number)
// create a string variable containing an integer string strNumber = "1000"; // cast the string to an integer int intNumber = int.Parse(strNumber);
Casting (converting) to a real
     

Often it's useful to convert a string representation of a number (e.g. one typed in by the user) to a decimal number for use in calculations (real data type)

This example converts the string "1.1234" to the real value 1.1234

# Create a string variable containing a real value str_number = "1.1234" # Cast the string to a real float_number = float(str_number)
// Create a string variable containing a real value strNumber = "1.1234"; // Cast the string to a real floatNumber = double.Parse(strNumber);
Splitting a string into an array of strings
    

It can be really useful to split one string into lots of smaller parts whenever you see a particular character.

This example will split the string "red,green,blue" into an array of smaller strings: "red", "green" and "blue"

It will then display the first colour: red

# Create a string storing comma separated values colours = "red,green,blue" # Split the values into an array of strings parts = colours.split(",") # Get the first value in the array print(parts[0])
// Create a string storing comma separated values string colours = "red,green,blue"; // Split the values into an array of strings string[] parts = colours.Split(","); // Get the first value in the array Console.WriteLine(parts[0]);
Getting part of a string (substring)
   

A string is some text. A substring is part of a bigger string. For example, "name" is a substring of "Hello, what's your name?".

This example creates a variable containing the string "What is your name" then displays the substring of the 5th and 6th characters ("is")

 

message = "What is your name?" # Get the 5th and 6th characters print(message[5:7])
string message = "What is your name?"; // Get the 5th and 6th characters Console.WriteLine(message.Substring(5,2));

Loading...