Code Companion
Java

Programming technique · B2.1.2

String manipulation

A string is a sequence of characters. Java provides methods for measuring, locating, extracting and creating changed text while the original String value remains immutable.

A string is a sequence of characters. Python provides functions, methods and slicing for measuring, locating, extracting and creating changed text while the original string value remains immutable.

IB DP CS standard B2.1.2: Construct programs that identify, extract and manipulate substrings, including alteration, concatenation and replacement.

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.
replace(old, new)Replacement copyReturns a new String; the original value is unchanged.
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.replace(old, new)Replacement copyReturns a new string; the original value is unchanged.
text.upper()Uppercase copyStrings are immutable; store or use the returned value.

Find a separator, then extract

Alter text by building a new string

Because strings are immutable in both languages, “changing a string” normally means creating a new string value from extracted pieces, concatenation or a replacement operation.

Check your understanding

Answer each question before opening the model answer.

  1. For the text "Miraflores", what does the range 0 to 4 return when the end index is excluded?

    Reveal model answer

    Mira, because indexes 0, 1, 2 and 3 are included and index 4 is excluded.

  2. What does indexOf/find returning -1 mean?

    Reveal model answer

    The requested substring was not found. Check that result before using it as an extraction position.

  3. Does replace() modify the existing string object in place?

    Reveal model answer

    No. In both examples it returns a new string value; store or use that returned value.

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, rebuild the sentence using substring extraction and concatenation, and output the revised sentence. Your program should respond clearly when the word is not present.

Scaffold available