Check out a free preview of the full Introduction to Elm, v2 course

The "Strings" Lesson is part of the full, Introduction to Elm, v2 course featured in this preview video. Here's what you'd learn in this lesson:

Richard introduces the process of manipulating strings in Elm.

Preview
Close

Transcript from the "Strings" Lesson

[00:00:00]
>> Richard Feldman: Manipulating values. All right, so here we're gonna talk about another four concepts. We're gonna talk about strings, let-expressions, lists, and partial application. Let's start with strings. So strings in Elm look pretty similar to JavaScript in terms of having quotes around them. One difference is that to combine strings you use plus, plus instead of plus.

[00:00:21]
Elm's big on being explicit and using different things for different concepts, so you use plus, plus for strings and plus is only for addition. So in JavaScript plus has two meanings, it can be used either to add numbers or it can be used to combine strings. Elm, those are separate.

[00:00:37]
Plus is only for addition. Plus, plus is for combining strings. We can use this plus, plus back in our pluralized function from earlier when we had the quantity and the singular and the plural. We can use this to make it so that it's going to display the number in addition to the singular or a plural.

[00:00:56]
So instead of just saying leaf or leaves, this version is going to return five leaves or one leaf. Now this isn't quite right yet because as I said previously plus, plus is for combining two strings. But quantity here is actually not a string, it's a number. So this part will work over here where we have a space plus, plus a string cuz those are both strings.

[00:01:23]
But this one will not. So, what we do to convert it to a from an Int to a string is we use String.fromInt quantity, and everything else remains the same. So String.fromInt says give me an integer and I will turn it into a string for you. It's similar to ToString in JavaScript, except that Elm has some special purpose functions for converting from different types of values to strings.

[00:01:46]
So there's a String.fromInt. There's a String.from float. There's other ways to convert different values to different types, but in our case quantity should be an integer. So we're gonna use String.fromInt. Also worth noting that you don't need any parenthesis to disambiguate here. Because the plus, plus actually implicitly groups things on either side of it.

[00:02:06]
So everything to the left of this plus, plus is gonna be one expression. Everything to the right of it is gonna be another expression. The same is true of this other plus, plus. Everything to the left of it is gonna be one expression, and everything to the right is gonna be another.

[00:02:19]
So anytime you see in-fix operators such as plus, plus, or the normal arithmetic ones like plus, minus, divided by and such. You can imagine that each of them implicitly creates parenthesis around everything to left and then everything to the right. So this whole thing is one expression that's going to evaluate to a string.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now