QA Friday 2016-Feb-05
Take Up Code - A podcast by Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes
Categories:
Are strings also a collection? And how are characters represented? This episode explains that strings do in many ways represent a collection of characters. Many programming languages will allow you to work with the individual characters just like you would work with individual items in an array. Some languages like C# have immutable strings which means that once a string is constructed, then it can’t be changed. You might think you’re changing a C# string but in fact all you’re doing is creating a new string with the modified value. The traditional method for determining the end of a string is to place a null value at the end. This is called a null terminated string. But what do you do if you want embedded null characters inside your string? My recommendation is that you don’t do this and instead select a different collection such as an array or vector of characters. The final topic that this episode explores is how character values are represented. You’ll learn about ASCII (American Standard Code for Information Interchange) and Unicode and how Unicode encodes characters. A common encoding used today is called UTF-8. Listen to the full episode or read the full transcript below. Transcript Thank you for your review and comments. This is one of the best reviews yet and it gives me a chance to discuss the promotions and explain how they fit into the episodes. First of all, producing a show like this is a lot of work. Even a ten minute episode takes me about three hours to prepare, record, edit, and publish. I love doing this and comments from you, my listeners, are very rewarding. The comment above made my day. Just knowing that you’re getting value out of these episodes is amazing. There’s so much more that I can help you with in the live classes which is why I’m sponsoring the show myself for now. In general, I’ll use the sponsor sections to let you know about products and services that will benefit you. I’ve already had people ask about sponsoring the show but I wasn’t interested because the advertisement was not related to programming. I hope you understand. I also hope to get some more variety in the sponsor segments eventually. For right now though, sponsors want to know what the show has to offer. So adding my own sponsor segment is a great way to get the process started. Okay on to the question this week. This question comes from the live class last week and I talked about this for at least a half hour. I don’t always know what you’re struggling to understand. If you’re having difficulty with something, then others are probably having the same trouble. Taking the time to ask a question is a great way to get an answer to your situation and help others too. Episodes 39 through 44 were all about collections. A string most closely resembles an array of characters and in fact you can think of a string as a collection of characters. Most languages give you the ability to enumerate the characters in a string either through special methods or the same as any other collection. The characters are ordered as you’d expect. Some languages like C# treat strings as immutable. This means that if you ever need to change a string, you need to create a whole new one. This is not always obvious and C# makes it easy to perform actions that appear to be changing a string. If you have a rather large string or a string that you’re building in pieces by adding a few characters at a time, then it’s easy to end up with some inefficient code. Adding just one character to the end requires allocating a whole new section of memory, copying over all the existing characters, and then adding the single character to the end. And if you then need to add another character, your program will do this all over again. You need to build a string in C#, there’s a class just for this purpose called the StringBuilder. You construct a StringBuilder and then b