If you're seeing this message, it means we're having trouble loading external resources on our website.

If you're behind a web filter, please make sure that the domains *.kastatic.org and *.kasandbox.org are unblocked.

### Course: AP®︎/College Computer Science Principles>Unit 3

Lesson 4: Strings

# String operations

Computer programs don't just store strings, they also manipulate them. With string operations, we can chop strings up, mash strings together, or transform strings in all sorts of ways.

## Combining strings

We love coming up with new lingo in computer programming land. One word you’ve probably never used before is concatenation, and it’s our fancy way of describing how we combine strings together.
This is what string concatenation looks like in JavaScript:
``````var name = "Winston";
var greeting = "Hello, " + name;``````
The `greeting` variable stores the result of concatenating two strings; chaining the two strings together. In fact, the word concatenation means “chain together” - it comes from Latin con- (“together”) and catenare- (“chain”).
We can chain any number of strings together:
``````var name = "Winston";
var title = "Sir";
var greeting = "Hello, " + title + name;``````
Notice we’re concatenating quoted strings with variables. We call the quoted strings string literals, since they’re literally a string, while the variables represent a string.
✏️ The program below displays the result of that code. Do you notice something not quite right? Can you fix it?
📝 See similar code in: App Lab | Snap | Python
Imagine our program starts with this line:
``var nickname = "SplashyPants";``
Select the line of JavaScript code that successfully stores the greeting "Ahoy, SplashyPants!":

## Slicing a string into substrings

We can also do the opposite of concatenation: we can slice a string and just store that slice. We call that slice a substring.
One way to slice in JavaScript is with the `substr()` command. It takes two parameters, start position and length. To use it, we call the command on the string itself:
``"Harry Potter".substr(0, 5);``
In JavaScript and most languages, the first position of the string is index 0. Let's visualize the indices of this string:
HarryPotter
01234567891011
That line of code starts at index 0, "H", and returns the 5-character long string "Harry".
An off-by-one error is a common kind of logic error that can happen when you're dealing with indices and accidentally use too high or low an index. Whatever language and command you're using, make sure you understand exactly how that command works, and always check your output.
✏️ The program below stores multiple substrings from one string (a phone number). Try to edit the code to store a third substring (the extension number).
📝 See similar code in: App Lab | Snap | Python

## Other string operations

Combining and slicing strings are just two ways we can manipulate string variables. Depending on what you're trying to do in your program, you may want to modify strings in other ways.
Here's a short list of common string operations across languages:
OperationJavaScript example
Finding the position of a character in a string`"hello".indexOf("e")`
Converting a string to all lowercase or uppercase`"HI".toLowerCase()`, `"hi".toUpperCase()`
Reporting the length of a string`"alphabet".length`

## Chaining multiple operations

Let's say that we want to both convert a string to uppercase and slice a substring out from it.
One way to do that is a single operation at a time:
``````var address = "329 Baltimore Lane";
Another option is to chain operations together:
``````var address = "329 Baltimore Lane";
That second line first calls the to `upperCase()` function, which returns "329 BALTIMORE LANE", and then immediately calls `substr()` on that uppercased string.

## Pseudocode for string operations

Since many programming languages use procedures for string operations (instead of operators like `+`), that is how you will likely see them in pseudo-code.
For example, here's a description of a concatenation procedure:
PseudocodeDescription
`CONCAT (str1, str2)`Concatenates `str1` and `str2`, returning the combined string.
The following line of pseudocode uses the `CONCAT` procedure to store "DogHouse" in the variable `home`:
``home ← CONCAT ("Dog", "House");``
A slicing operation only operates on a single string but needs parameters to figure out where to slice. Thus, a description for a slice procedure might look like this:
PseudocodeDescription
`SLICE (str, start, length)`Returns the substring of characters from `str`, starting at the `start` index and containing `length` characters total. Strings start at index 1.
This line of pseudocode uses the `SLICE` procedure to store "Dan" in the variable `nickname`:
``nickname ← SLICE ("Daniel", 1, 3);``
⚠️ Notice that this pseudocode assumes the first index of the string is 1, whereas the first index of strings in JavaScript and many other languages is 0. Pay close attention to the description of any procedures involving indices.
What is the pseudocode equivalent of this JavaScript?
``"San Diego".substr(0, 3)``

## Want to join the conversation?

• They say that most programming languages have the index from 0 up, and pseudocode has the index from 1 up.
But when checking our understanding in converting from javascript to pseudocode, they insist that the 0 in 0-3 becomes 1, but that the 3 in 0-3 stays as 3.
But this is not the pseudocode equivalent to javascripts 0-3, as that would move everything by one, not just the first, the pseudo code would be 0-4, becuase in javascript, 0-3 would mean the first 4.
So this must be wrong.
• I think you're misunderstanding. 3 is the number of characters you want to be in the slice, not the index of the last character you want to be in the slice; basically, it's the length of the output string.

The reason indexes usually start from 0 is because, logically, that would be the offset of the first character in a word from the beginning - however, pseudocode makes the index of the first letter 1, because we're used from counting from 1. However, the same thing doesn't apply to length, because it wouldn't make sense for a length of 0 to return anything other than 0 characters, a length of 1 to return anything other than 1 character, and etc.
• Why would someone store a number as a string rather than just the numerical value?
• It depends on what you intend to use the number for. Let us consider a phone number, such as (512)-487-3644. This phone number can also be represented as 5124873644. In this instance, you could either store the number as a string or as an integer; what would you choose?

If you were to store it as a string, you would have a much easier time formatting it. For example, if you wanted to display the number to a user as "(512)-487-3644" then you would need to add parentheses and hyphens (assuming you stored it originally as 5124873644) which would be easier to do with a string. On the other hand, if you intended to perform arithmetic with the number, then that would be much easier to do if the number was stored as an integer. However, it's a phone number - so you're unlikely to ever perform arithmetic on it.

In this case, a string would be the more sensible data type. This is only one example, I implore you to keep considering varying situations in which either a string or numeric data type would better suit the problem at hand.
• Is there also a command to change all words to camel-case?
• For the first exercise, how do you add spaces to Hello, SirWinston?
• Try to concatenate a string that contains an empty space (" ") between the other strings.
• When we use the substr command and put in values for the parentheses, the first value is the start position and that can be calculated by numbering each of the characters starting with zero. But when we enter the second value, the length, do we start counting with the FIRST value, or the value after?

For example, say we type in-
var name = Amelia Earhart!
var lastname = name.substr(7, 7);
println(lastname);

Would it print Earhart or Earhart! ?

Thanks!
• It would print Earhart
starting from 7th postion it prints 7 characters

note: The string needs to be included inside double quotes
var name = "Amelia Earhart!";
• How would you store a variable depending on what the user inputs, rather than just coding it into the software?
• There are various ways to achieve this functionality, and in many instances you'll be making use of a Graphical User Interface to collect user input.

However, a simple way to accomplish this in JavaScript would look like:

``let userInput = '';userInput = window.prompt();``

This method will prompt a user for input with a pop-up, and the user's input will be assigned to the variable `userInput` as a string.
• I noticed that several of the exercises include the description of what the pseudocode procedure does. For example, one of the questions gives you a description of what concatenate(string1,string2) means, and then asks you a question about the pseudocode using that procedure. Will that description be provided on the AP exam? Or is it necessary to memorize all the procedures (like STRING, CONCAT, SLICE, etc) in pseudocode?
• You could look at some practice tests to check,
``text("thanks", mouseX, mouseY);``