Sat. Jan 21st, 2023

String Handling Functions

You are expected to understand the following terms, be able to explain them, and be able to recognise their use in written code.


Concatenation is the process where two or more strings are joined together. For example, consider the following application:

Console.Write("Enter your name: ");
string name = Console.ReadLine();
Console.WriteLine("Hello " + name);
//The third line prints one message on the screen
//The message is the word 'Hello ' plus the name that
//has been entered. This is an example of concatenation
//In C# you concatenate strings using +

name = input('Enter your name:')
print('Hello', name)
#or the following
print('Hello ' + name)
#When printing, strings can be concatenated using commas
#Elsewhere in code, as with C# simply use +


The length function returns a value that contains the number of characters in a string. For example, the string “HELLO” has a length of 5. As with arrays, the numbering of the characters starts at position 0; therefore in the word ‘HELLO’ there are characters at positions 0, 1, 2, 3 and 4. Note that the position of the final character is ‘length – 1’.

string word = "Bonjour";
int wordLength = word.Length;
char lastLetter = word[word.Length - 1];
//wordLength is set to 7
//lastLetter is set to 'r'

word = "Bonjour"
wordLength = len(word)
lastLetter = word[len(word) - 1]
#Same as above example


As shown above you can access characters (and substrings) from within a string. Remember that positions within strings are 0-based.

string phrase = "How are you today?";
char firstCharacter = phrase[0];
string firstWord = phrase.SubString(0,3);
//firstCharacter is set to 'H'
//firstWord is set to the substring starting at pos 0, 3 chars long
//So firstWord is set to 'How'

phrase = "How are you today?"
firstCharacter = phrase[0]
firstWord = phrase[0:2]
#firstCharacter is set to 'H'
#firstWord is set to the substring (slice) that starts at 
#position 0, and ends at position 2; i.e. 'How'

String Conversions

You will regularly find the need to convert between strings and numerical formats. Remember that any data entered is of ‘string’ type, and if you are expecting a numerical value, you must first convert the text into a number.

Integer/float to string

Although C# usually recognises attempts to use a numerical value in a textual context and makes the appropriate conversions, you can still do this manually to avoid any possible issues. Python requires you to make the conversions.

int anInteger = 5;
float aFloat = 3.57f;
string integerAsString = anInteger.ToString();
string floatAsString = aFloat.ToString();
//In C# simply use the method .ToString() to convert
//from any value to a string

anInteger = 5
aFloat = 3.57
integerAsString = str(anInteger)
floatAsString = str(aFloat)
#In Python, use the function str() to convert a value
#into a string

String to integer/float

All text read from files, the keyboard or from text controls on a page is of data-type string. This means you are unable to perform mathematical operations with these values. If you need to do this, you must convert the input into a numerical format first.

string text = Console.ReadLine();
int textAsInteger = Convert.ToInt32(text);
double textAsDouble = Convert.ToDouble(text);
//NB C# does not have a Convert.ToFloat method. If you are expecting
//a floating point value, you use a double instead of float
//ToInt32 refers to a 32bit integer value. If this is not big enough
//There is also an option for ToInt64

text = input()
textAsInteger = int(text)
textAsFloat = float(text)
#In the same way that Python casts to string using a function
#called str(), there are functions called int() and float()
#to perform the opposite conversions