Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In the previous movie we learned how to enter string data either manually by typing it into the sketch or by importing the text file. If you want to be able to work with strings, Processing has a wide array of functions. I'm going to demonstrate a few of them to you here. But you should know that there is a lot more that's available, specifically in terms of searching for functions and that one uses Regular Expressions. In fact, if you're interested in learning more about Regular Expressions, you should see Lynda.com's course on that topic specifically. It would come in particularly useful for working with string data in data visualization.
What I want to show you now is just two functions. One is on trimming empty space from the beginning and end of strings and another one is for converting numbers into strings. I'm going to start by simply putting the name of this file up here as a comment. And then I'm going to first demonstrate the trim function. And the way this works, is you take a string. I'm creating a new string here I'm going to call it quote and you see I've got some extra space at the end and at the beginning.
I am actually going to fix the capitalization in there. And if I do a print line on that one, you'll see that, it prints with the extra space. You can't tell that there's space after it, but you can certainly see the space before. The Trim function is a great way of dealing with that. In this case, what I'm going to do is I'm going to simply say that the quote because I'm going to modify the string itself trim(quote); and when I runs that, the empty space is now disappeared at the beginning and in the end.
Now in an ideal world, you would have done this cleaning before you brought in any text from an external source. But sometimes it slips through so this is a way to solve the problem once the data is in Processing. The other thing, I want to show you is how to take a number, a numerical variable and turn it into a string which sometimes is necessary for some of the visualizations that we would be creating. Processing has several functions for dealing with this. I'm only going to show you the most basic one, because all the others have very slight variations on it. And what I'm going to be doing is using one called nf and you can think of that as number format or numerical format.
I'm going to create a variable, an integer variable, that I will call a and I'll give a number 798. I can print line and there's my variable there at the bottom. Now if I want to make it a string, what I do is this. I do String and I'm actually going to create a new variable, I am going to call it Capital A. Processing is case -sensitive so I can do this, and then I can you put nf, for a numerical format give the variable name for the original number which in this case is a. And then the number of digits I want in this particular string.
I want to 3 because there are three digits in it. I am going to do a little print line (A); and you can see what it looks like. Now what's interesting here is it looks exactly the same, except the second one is a string. The first ones is a number, the second one is a string. I can show you, I can make it very different. If I make it, so that it's 10 digits. And now we have a bunch of leading zeros onto this. Processing has other functions for adding positive or negative signs and for dealing with decimal places and padding on and off, and I simply am going to direct you to the reference, they work exactly the same way as this one.
But the idea here is that you can take a number and turn into a string which makes it useful for the other operations you can be doing with text.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 98414 Viewers
61 Video lessons · 85697 Viewers
71 Video lessons · 69624 Viewers
56 Video lessons · 101948 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.