Mon. Jan 23rd, 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

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

C#
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 +


Python
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 +

Length

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’.

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


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

Position

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

C#
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'

Python
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.

C#
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


Python
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.

C#
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


Python
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