Phantasmal MUD Lib for DGD
|
Phantasmal Site > DGD > DGD Reference Manual (legacy) > Simple String Manipulation 3.4 Simple String ManipulationSome time back, we called the status() function, and extracted a result which wasn't a number at all. It was a string, which is a set of character values, one after the other. Of course, it doesn't usually look that way. It usually looks like a word or phrase or paragraph. In DGD, just like in C, strings use what's called ASCII code. Computers represent everything with numbers internally. So every printable character (and many special characters with no printable representation) must be assigned a number. ASCII is one way to assign those numbers. A DGD string is just a sequence of numbers, each representing a letter, number or other printable character. A DGD string can also hold the empty string, which is written as "". That's a sequence of zero characters, and it just means "no string here". The string can also hold the special value nil. Every string holds nil when you first declare it. So do certain other data types we'll talk about later. nil isn't a valid string, it's just a placeholder meaning that the string isn't usable yet — you have to assign a value to it first. So go ahead and type code "Hello world!". You should see a result something like $15 = "Hello world!". The quotation marks around it mean that it's a string. In LPC, a string is always double-quoted. Just as you can use operations like + and * on numbers, you can also use certain operators on strings. For instance, the + operator will concatenate two strings into a single longer one. Try typing code "Hello" + "World!". Note that you don't automatically get a space in between, so you should have seen a result like $18 = "HelloWorld!". Remember that the string is made of a sequence of numbers which represent characters. In LPC you can extract one of these numbers with the square-brace operator. For instance, type code "A"[0]. You should see a result like $20 = 65. That's because 65 is the ASCII encoding for an uppercase A. Try using strings other than "A" and you can see more ASCII encodings. You'll notice that all the uppercase letters are ordered alphabetically within themselves so B is 66, C is 67 and so on. The lowercase letters do the same thing. The zero in braces, above, specifies what offset in the string to examine. For instance, if you type code "FANDANGO"[4], you'll get 65 also. That's because the number is square brackets is what character of the string to examine, and character four corresponds to the fifth character, which is an uppercase A in the word "FANDANGO". The string's characters are numbered in order, starting with zero. You can also extract more than one character from a string. But that can't just be an ASCII number. So if you extract more than one character from a string, LPC gives you another string. Try typing code "Big Bertha"[0..2]. You should get the result "Big". That's characters number zero through two — the first through third characters. You'll find that if you substitute You can also find the length of a string in characters. For instance, type code strlen("Bob's Aunt Polly"). You should get a result like $24 = 16. That's because the string contains sixteen characters — that includes the apostrophe and both spaces. Character counting with strlen() includes punctuation and other special characters as well as letters and numbers.
|