Welcome to the World of Strings!
In Computer Science, we use the word string to describe any piece of text. It could be a single letter, a whole sentence, or even a password with numbers and symbols. In this guide, we are going to learn how to "handle" strings—which basically means slicing, joining, and measuring them to make our programs do exactly what we want!
Think of a string like a necklace made of beads. Each bead is a character (a letter, space, or symbol). By the end of these notes, you’ll be a pro at counting those beads, picking out specific ones, and even turning them into numbers. Let's dive in!
1. Measuring a String: Length
The length operation tells us exactly how many characters are in a string. This includes letters, numbers, punctuation, and even spaces!
How it works:
Imagine the string "Pizza Pie".
P (1), i (2), z (3), z (4), a (5), [space] (6), P (7), i (8), e (9).
The length is 9.
Quick Review:
Don't forget the space! Computers see a space as a character just like the letter 'A'. If you forget to count the spaces, your length will be wrong.
Key Takeaway: Length gives you the total count of every single character in the string.
2. Finding a Character: Position
Position (sometimes called indexing) allows us to find where a specific character is located within the string.
Don't worry if this seems tricky at first... computers are a bit strange. They don't start counting at 1; they start at 0! This is called zero-indexing.
The "Locker" Analogy:
Imagine a row of school lockers. The very first locker is labeled 0, the second is 1, and so on. To find the "Position" of a letter, you just find its locker number.
Example: "CODA"
C is at position 0
O is at position 1
D is at position 2
A is at position 3
Common Mistake: Many students think the last letter of a 4-letter word is at position 4. It's actually at position 3 because we started at 0!
Key Takeaway: The first character is always at position 0.
3. Taking a Slice: Substring
A substring is a smaller part of a larger string. It’s like taking a "sub-section" of the text.
To get a substring, you usually need to tell the computer two things:
1. Where to start (the position).
2. How many characters to take (the length of the slice).
Example: Let's take a substring of "Chocolate".
If we start at position 0 and take 4 characters...
C (0), h (1), o (2), c (3)
The result is "Choc".
Key Takeaway: Substring lets you "cut out" a specific piece of text from a bigger string.
4. Joining Together: Concatenation
Concatenation is just a fancy word for joining two or more strings together to make one long string. Think of it like snapping two LEGO bricks together or using a glue stick.
Step-by-Step Example:
1. Take string A: "Super"
2. Take string B: "Hero"
3. Concatenate them.
4. Result: "SuperHero"
Did you know? If you want a space between the words, you have to add it yourself! Concatenating "Hello" and "World" gives you "HelloWorld". To get "Hello World", you must join "Hello" + " " + "World".
Key Takeaway: Concatenation + Glue = Joined Text.
5. Characters and Codes
Computers don't actually understand letters; they only understand numbers. Every character has a special "ID number" called a character code (using systems like ASCII or Unicode).
Convert Character to Character Code
This operation takes a letter and tells you its secret number.
Example: In ASCII, the character 'A' converts to the code 65.
Convert Character Code to Character
This does the opposite! You give it a number, and it tells you the letter.
Example: The code 97 converts to the character 'a'.
Memory Aid: A = 65. If you remember that capital 'A' is 65, you can figure out 'B' is 66, and 'C' is 67!
Key Takeaway: These operations are like a translator between the human alphabet and the computer’s number system.
6. String Conversion Operations
Sometimes we have a number stored as text (a string), but we need to do math with it. Or, we have a number result that we want to display as part of a sentence. We use conversion operations for this.
1. String to Integer: Converts "10" (text) into 10 (a whole number you can add or subtract).
2. String to Real: Converts "3.14" (text) into 3.14 (a number with a decimal point).
3. Integer to String: Converts the number 50 into text "50" so it can be joined to a sentence.
4. Real to String: Converts 9.99 into text "9.99".
Why is this important?
If you try to "add" two strings like "5" + "5", concatenation might give you "55"! You must convert them to integers first if you want the answer to be 10.
Quick Review Box:
- Integer = Whole number (e.g., 7)
- Real = Decimal number (e.g., 7.5)
- String = Text (e.g., "7")
Key Takeaway: Conversion lets you change data types so you can use numbers for math and strings for display.
Summary Checklist
Can you explain these 7 things?
1. Length: How many characters?
2. Position: Where is the character? (Remember 0!)
3. Substring: A slice of the string.
4. Concatenation: Joining strings together.
5. Character to Code: Letter \(\rightarrow\) Number.
6. Code to Character: Number \(\rightarrow\) Letter.
7. Conversions: Changing between String, Integer, and Real types.