String Handling
- Strings are a sequence of characters, such as letters, numbers, and symbols
- They are used to represent text in a computer program
- String handling refers to the various operations that can be performed on strings
Length
- The length of a string is the number of characters it contains
- In Python, the len() function is used to find the length of a string
- In Java, the length() method is used to find the length of a string
- In Visual Basic, the Len() function is used to find the length of a string
Substring
- A substring is a portion of a string
- In Python, the substring can be obtained using the slicing operator [:]
- In Java, the substring() method is used to obtain a substring
- In Visual Basic, the Mid() function is used to obtain a substring
Upper
- The upper() method converts all characters of a string to uppercase
- In Python, the upper() method is used to convert a string to uppercase
- In Java, the toUpperCase() method is used to convert a string to uppercase
- In Visual Basic, the UCase() function is used to convert a string to uppercase
Lower
- The lower() method converts all characters of a string to lowercase
- In Python, the lower() method is used to convert a string to lowercase
- In Java, the toLowerCase() method is used to convert a string to lowercase
- In Visual Basic, the LCase() function is used to convert a string to lowercase
Pseudocode example:
string phrase ← "Save my exams"
length ← LENGTH(phrase)
substring ← SUBSTRING(phrase, 5, 2)
upper ← TO_UPPER(phrase)
lower ← TO_LOWER(phrase)
Python example:
phrase = "Save my exams"
length = len(phrase)
substring = phrase[4:6]
upper = phrase.upper()
lower = phrase.lower()
Java example:
String phrase = "Save my exams";
int length = phrase.length();
String substring = phrase.substring(4, 6);
String upper = phrase.toUpperCase();
String lower = phrase.toLowerCase();
Visual basic example:
Dim phrase As String = "Save my exams"
Dim length As Integer = phrase.Length
Dim substring As String = phrase.Substring(4, 2)
Dim upper As String = phrase.ToUpper()
Dim lower As String = phrase.ToLower()
Position of First Character
- The first character of a string can be at position zero or one, depending on the programming language
- In Python and Java, the first character of a string is at position zero
- In Visual Basic, the first character of a string is at position one
Worked example
The function Length(x) finds the length of a string x. The function substring(x,y,z) finds a substring of x starting at position y and z characters long. The first character in x is in position 1.
Write pseudocode statements to:
- Store the string “Save my exams” in x
- Find the length of the string and output it
- Extract the word exams from the string and output it
[6]
X ← "Save my exams" |
[1] for storing string in X. | |
OUTPUT Length(X) |
[1] for calling the function length. [1] for using the correct parameter X. | |
|
||
OUTPUT SubString(X,Y,Z) |
[1] for using the substring function. [1] for correct parameters. [1] for outputting length and substring return values. |
Exam Tip
- Remember that the length of a string is not the same as the index of the last character in the string
- Be careful when counting positions in a string, as the first character can be at position zero or one depending on the programming language