Lesson 12: Strings
In our previous lesson we discussed functions. In this lesson, we’ll take a detailed look at strings and some related functions that are really common.
What are Strings?
You’ve seen strings in just about every previous lesson so far. A string is simply a “string” of character, generally represented between two double-quotes (“”). Strings are used to hold most non-constant/non-static text in programs. There are sometimes other data types to hold larger chunks of text, but often times they are just a type of string.
In nearly every language, strings have many built-in functions which provide you various actions that allow you to do various things. We’ll take a look at a few of the more useful and common ones.
Length
The Length function (also called count, strlen, or a property in an object-oriented language). This function simply returns the number of characters that is in the string. For example, if the string is “bob”, Length would return 3. If the string was “this is a duck”, you would get 14 (remember, spaces are also characters).
Substring
The Substring function (often shortened to substr), is a function that takes a string and will return a part of a string based on a starting index that you provide and a length.
An index is a zero-based number that specifies the position of a specific character (or element in an Array). So, for example, in the string “abcdef”, a has an index of 0, d has an index of 3, etc.
The Substring function uses these values to return parts of a string. The specifics of Substring vary from language to language, but most will take an index value that is less than the value of returned by Length, and a positive value for a length. Some languages will also allow you to pass a negative value for the length as well.
If you have a positive value for the length, it will start at the starting index and get the number of character equal to length, or as many as it can before it runs out of characters. If you have a negative value, it will still start at the same position, but it will cut that many characters off the end of the string.
For our concerns, we’ll say the syntax for Substring is:
function String Substring(String str, Integer start, Integer length)
Here are some examples:
Substring("abcdef",0,6) // "abcdef"
Substring("abcdef",2,2) // "cd"
Substring("abcdef",4,-1) // "de"
Substring("abcdef",0,-2) // "abcd"
IndexOf
IndexOf will return the index of the first instance of a specified character. As usual, the specifics very a lot, but in most languages if the character (or characters) you’re looking for in the string aren’t there, it’ll return -1. Also, some languages you can only have one character to search for, while others will take any number of characters (as a string).
We’ll say that our syntax of IndexOf is:
function Integer IndexOf(String str, Integer index)
And some examples:
IndexOf("abcdef","a") // 0
IndexOf("abcdef","b") // 1
IndexOf("abcdef","e") // 4
IndexOf("abcdef",z") // -1
Other Functions
Length, Substring, IndexOf are pretty universal throughout all programming languages. There are usually many many more in most languages.
Concat
Some languages require the use of a Concat (concatenation) function to put two strings together, instead of just adding them together like other languages.
Lowercase
Will take a string and return a string with all of the characters lowercase.
Uppercase
Will take a string and return a string with all of the characters uppercase.
Split
Will take a string and a “delimiter” (a character used as a seperator, such as a comma) and will break the string up into an array at those delimited points.
Example syntax:
function Array Split(String str, String del)
Examples:
Split("a,b,c",",") // {"a","b","c"}
Split("a|b|cd|efg|hi","|") // {"a","b","cd","efg","hi"}
Split("Bob is crazy and insane"," ") // {"Bob","is","crazy","and","insane"}
In this lesson we took a look at some very useful string functions that are used very commonly in practical applications. In our next lesson, we'll take a look at the difference between object-oriented and procedural programming.
Leave a Reply
If you haven't posted an approved comment before, your comment will not show up until approved.




