Programming Companion
Java

Programming technique

String manipulation

A string is a sequence of characters. Java provides methods for measuring, locating, extracting and changing text without changing the original string value.

A string is a sequence of characters. Python provides functions, methods and slicing for measuring, locating, extracting and changing text without changing the original string value.

IB DP CS standard B2.1.2: Construct programs that can extract and manipulate substrings.

Useful String methods

Useful string operations

Every character has a zero-based index
M0
i1
r2
a3
f4
l5
o6
r7
e8
s9

substring(0, 4) includes indexes 0–3, so the result is Mira.

city[0:4] includes indexes 0–3, so the result is Mira.

MethodPurposeImportant detail
length()Number of charactersThe first character is still at index 0.
charAt(index)One characterReturns a char.
substring(start, end)Part of a stringIncludes start but excludes end.
indexOf(text)First position of textReturns -1 when not found.
toUpperCase()Uppercase copyStrings are immutable; store or use the returned value.
OperationPurposeImportant detail
len(text)Number of charactersThe first character is still at index 0.
text[index]One-character stringA position outside the string raises IndexError.
text[start:end]Part of a stringIncludes start but excludes end.
text.find(value)First position of textReturns -1 when not found.
text.upper()Uppercase copyStrings are immutable; store or use the returned value.

Find a separator, then extract

Zero-based indexing

The first character is index 0, not index 1.

End index excluded

substring(0, 4) returns characters at indexes 0, 1, 2 and 3.

End index excluded

text[0:4] returns characters at indexes 0, 1, 2 and 3.

Check before extracting

A missing separator or an invalid character position can make the result wrong or cause a runtime error.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Initial and Surname

Challenge ID: PC-T03-C01 · Standards: B2.1.2

Ask the user for a forename and surname. Output the first initial followed by the surname in uppercase. For example, ben white should become B WHITE.

Airline Ticket Code

Challenge ID: PC-T03-C02 · Standards: B2.1.2

Ask the user for two city names. Output the first four characters of each city in uppercase, separated by a dash. For example, London and Madrid should produce LOND-MADR. State what your program expects if a city name contains fewer than four characters.

Two city names being shortened into an airline ticket code.

Name Separator

Challenge ID: PC-T03-C03 · Standards: B2.1.2

Ask the user to enter a forename and surname on one line. Find the space and extract the two names into separate variables. Output each part with a clear label.

A full name being separated into forename and surname.

Word Extractor

Challenge ID: PC-T03-C04 · Standards: B2.1.2

Store the sentence "Quick brown fox jumps over the lazy dog." Ask the user which word should be removed. Find the word, remove it using substring operations and output the revised sentence. Your program should respond clearly when the word is not present.