Using characters and strings
Video: Using characters and stringsWhen we want to deal with text in our programs, characters, words, sentences, email addresses, we use the term strings to describe those kind of values. Now we've seen these already. Anytime you see words contained in sets of quotes, we're dealing with strings. The first line of code that we wrote was this alert message that used the string "Hello, world". This value contained in the double- quotes is referred to as a string literal, just like the number 5 or the number 1,000,000 would be a numeric literal.
- Where to go from here
Viewers: in countries Watching now:
Finally, the course compares how code is written in several different languages, the libraries and frameworks that have grown around them, and the reasons to choose each one.
- Writing source code
- Understanding compiled and interpreted languages
- Requesting input
- Working with numbers, characters, strings, and operators
- Writing conditional code
- Making the code modular
- Writing loops
- Finding patterns in strings
- Working with arrays and collections
- Adopting a programming style
- Reading and writing to various locations
- Managing memory usage
- Learning about other languages
Using characters and strings
When we want to deal with text in our programs, characters, words, sentences, email addresses, we use the term strings to describe those kind of values. Now we've seen these already. Anytime you see words contained in sets of quotes, we're dealing with strings. The first line of code that we wrote was this alert message that used the string "Hello, world". This value contained in the double- quotes is referred to as a string literal, just like the number 5 or the number 1,000,000 would be a numeric literal.
We can of course use this format to create variables. I could create a new variable called message and set it equal to the words "Hello, world". I could then use that variable and pop out another alert box. So we are using the string literal to create the variable called message and then writing that variable out. Now, notice that I'm not using double- quotes around message in this second alert statement because I don't want to write out the word message. I want to write out the value of the variable called message.
Let's say for example we want a phrase that contains both double quotes and single quotes. Well, there is no simple way to mark out the beginning and the end of this without doing something else here. If this is what we need, what we can do is what's called escaping the quotes. This would be the way that I'd have to write it. The entire string begins and ends with double quotes and that means if I want double quotes inside that string, I mark them by putting a backslash before the double quotes being used inside the body of the string itself.
Now, one of the great benefits of dealing with strings in most programming languages is they are smart. These variables go beyond just having a holding place for some characters. We can ask things of them. We'll get into this a lot later, but just to give you a basic example, let's say I create a new variable called phrase and set it to the words "This is a simple phrase." Well, one of the things I can do is I can use the name of that variable to access or get to information about it, such as how long is it.
I can also ask questions of it, like does another word exist inside it? I can convert it to upper or lowercase. As the most straightforward example here, I am going to write the line alert and then instead of just the name phrase, I am going to say phrase.length. This is allowing me to get to what is called the length property of this variable, and what's going to happen is this will pop up an alert box that will say in this case 24. We can actually ask questions of our string variables.
As we get deeper into creating and working with any language, you will find this is very common, but variables by themselves aren't dumb. They can actually give us more information about the contents and about what they're holding. Now we'll get deeper into strings a little later on, but that's enough to get us started.
Find answers to the most frequently asked questions about Foundations of Programming: Fundamentals .
Here are the FAQs that matched your search "" :
- Q: Using TextEdit with Mac OS 10.9 Mavericks?
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.