116: Data Types: Strings Part 3.

Take Up Code - En podcast af Take Up Code: build your own computer games, apps, and robotics with podcasts and live classes

Kategorier:

You need more than a bunch of numbers and logic to write an application. You need text and working with individual characters isn’t enough either. This episode continues explaining concepts important to the string data type with the following seven points: How do you search a string for another string or a pattern? How do you use a string as an index or key into a collection? How do you read and write strings as data streams? How do you use strings to transfer information such as HTML, JSON, and XML? How do you convert images to strings and then back again? How do you sort strings and control how numbers and special characters affect the order? How do you embed special symbols into strings such as line breaks, presentation information, and binary data? Listen to the full episode or you can also read the full transcript below. Transcript This episode continues the explanation of the string data type, what you can do with it, and many of the unique considerations that apply to strings. Listen to episodes 114 and 115 for the first two parts. Here’s the next seven points from #15 to #21. #15 How do you search a string for another string or a pattern? Most string data types will provide methods like findFirst or findLast that allow you to specify a string to search for. These can be a good choice when you’re fairly sure the text you’re looking for is somewhere in a larger string and you just need to verify that and find the exact location. Sometimes you just have one character that you’re interested in or any one of several characters. These could be special characters such as the angle brackets used in HTML, quotation marks that signal special data, or spaces or tabs. When you want to find the first or last of any one of several characters, then look for methods like findFirstOf or findLastOf. These methods also take a string to search for but instead of searching for the string as-is, they search for each character in the search string independently. Putting all this together, if you have the string “Stay with it.” and call findFirst with the string “it”, then you might expect the method to return the index of the word it. However, findFirst normally doesn’t know anything about word boundaries and will tell you that the string “it” first occurs inside the word “with”. That’s because, there’s an i and a t inside the word “with”. What if you call findFirstOf with the same string “it”? Now, you’re asking the string to find the first character of any of the characters in the search string. The method will return the index of the t in the word “Stay”. If you want to get more complicated search patterns, you can, but I’d suggest using another class called a regular expression or just regex for short. Regular expressions will need a future episode to explain fully. #16 How do you use a string as an index or key into a collection? This can be an extremely useful way to organize your data. When you define a keyed collection such as a dictionary, you define the types that will be used for the keys and for the values. Listen to episodes 43 and 44… for more information. Let’s say you want to load some information into memory that applies to countries. This information could be weather related such as the high temperature each day. The information will arrive in various orders but you want to be able to access the information related to a specific country very quickly later. It makes sense then to put the information into either a hashtable or a dictionary as you get it. But what do you use for the key? Why not use the standard two-letter country code that each country already has. Even two characters is still a string. Actually, you can have a string with just a single character. Or no characters at all. But in this case, we’ll have two character strings that we can use as the ke

Visit the podcast's native language site