Main content
AP®︎/College Computer Science Principles
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?
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:
H | a | r | r | y | P | o | t | t | e | r | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
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).
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:
Operation | JavaScript 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";
var upperAddr = address.toUpperCase();
var upperStreet = upperAddr.substr(4, 9);
Another option is to chain operations together:
var address = "329 Baltimore Lane";
var upperStreet = address.toUpperCase().substr(4, 9);
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:
Pseudocode | Description |
---|---|
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:
Pseudocode | Description |
---|---|
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.
🙋🏽🙋🏻♀️🙋🏿♂️Do you have any questions about this topic? We'd love to answer— just ask in the questions area below!
Want to join the conversation?
- Is there also a command to change all words to camel-case?(6 votes)
- From the author:Not in the JavaScript language, no. You could write a function like that as a nice coding challenge.
Here's a list of all the JS string methods:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String#Methods_2(6 votes)
- Why would someone store a number as a string rather than just the numerical value?(5 votes)
- 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.(7 votes)
- 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.(6 votes)- 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.(1 vote)
- For the first exercise, how do you add spaces to Hello, SirWinston?(3 votes)
- Try to concatenate a string that contains an empty space (" ") between the other strings.(6 votes)
- 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!(2 votes)- 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!";(2 votes)
- How would you store a variable depending on what the user inputs, rather than just coding it into the software?(2 votes)
- 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 variableuserInput
as a string.(2 votes)
- Psuedocode is cool! I just wish it wasn't so hard to learn. Could someone coach me?
text("thanks", mouseX, mouseY);
(2 votes) - So when you are writing a substrate command, you can use either the literal string (i.e. "Splashypants") or the variable name (i.e name) as the first part (like "Splashypants".subtr(1,3);) ? Is one preferred over the other?(2 votes)
- You would most likely use the variable name.
When you write code you generally want to avoid dealing with actual strings (something like "Splashypants") or numbers (something like 5).
So you define the Strings and numbers at the start of the code and then only work with variables. That way it's easier to understand what's happening, especially if you pick good variable names.(1 vote)
- 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?(2 votes)
- You could look at some practice tests to check,(1 vote)
- Chaining multiple operations
var address = "329 Baltimore Lane";
var upperAddr = address.toUpperCase();
var upperStreet = address.substr(4, 9);
is this right or this
var address = "329 Baltimore Lane";
var upperAddr = address.toUpperCase();
var upperStreet = upperAddr.substr(4, 9);(1 vote)- Hmm they both do the same, so it's a matter choice.
I personally would prefer the first variant, because both upperAddr and upperStreet depend on address, but could easily exist without their partner.
And if I built dependencies I want them to be as clear and simple as possible.(2 votes)