Using String Methods

Your inputs have arrived as strings, and most kinds of data can be represented as strings. Working with strings is the bread and butter of every programmer. Like Ruby, Crystal is well equipped for string processing. You’ve been using strings throughout this book, but there are more ways to create strings and more ways to work with them.

Crystal, like Ruby, supports multi-line strings:

 str = ​"What a beautiful mineral!"
 str1 = ​"What a
 beautiful mineral!"​ ​# multi-line string

Crystal also supports the use of the backslash to escape characters inside of strings. If you wanted to create the same str1 shown above while keeping it on one line in your code, you could instead type the following:

 str = ​"What a beautiful mineral!"
 str1 = ​"What a ​​ ​​beautiful mineral!"​ ​# multi-line string

Typically, you’ll want to escape the double quote (") and the backslash itself (\). You can also use numeric escaping. u followed by a hexadecimal number enables you to specify a Unicode character. For example, u2603 gets you a Unicode snowman.

Once you have strings, Crystal offers you many ways to process them:

 curr1 = ​"US Dollar"
 curr1[2..4] ​# => " Do"
 curr1.​reverse​ ​# => "ralloD SU"
 curr1.​size​ ​# => 9 # length or len do not exist
 curr1.​upcase​ ​# => "US DOLLAR"
 curr1.​capitalize​ ​# => "Us dollar"
 curr1.​includes?​ ​"la"​ ​# => true
 curr1.​count​ ​"l"​ ​# => 2
 curr1.​starts_with?​ ​"Us"​ ​# => false # case sensitive!
 curr1.​ends_with?​ ​"ar"​ ​# => true
 curr1.​index​(​"a"​) ​# => 7
 curr1.​sub​(​"ll"​, ​"l"​) ​# => "US Dolar"
 curr1.​gsub​(​/([aeiou])/​, ​"*​​\​​1*"​) ​# => "US D*o*ll*a*r"
 curr2 = curr1.​split​(​""​) ​# => ["U","S"," ","D","o","l","l","a","r"]
 curr2.​join​(​"-"​) ​# => "U-S- -D-o-l-l-a-r"

split and join are classics: split transforms a string into an array—we used it in our currency converter—while join goes the other way around, transforming an array into a string.

Strings are created on the heap. They’re immutable, which means you can’t change their content directly:

 s = ​"USD"
 s[2] = ​'s'​ ​# => Error: undefined method '[]=' for String

For efficiency, you should avoid creating extra strings. In the following snippet, both to_s, +, and string interpolation create a string on the heap, though interpolation is faster:

 rate = 0.84320536
 p "rate: " + rate.to_s # => "rate: 0.84320536"
 # string interpolation is more efficient:
 p "rate: #{rate}" # => "rate: 0.84320536"

You can also use a String builder and the << method:

 str = String.build do |io|
  io << "rate: " << rate
 end
 p str # => "rate: 0.84320536"

As your projects grow more complex, efficient string handling will matter even more.

Your Turn 2

a. Split the following string of mineral names into an array where all of the names are uppercase: gold;topaz;apatite;wolframite;calcite;diamond (Hint: Use the map method you learned about in Chaining Methods).

b. Strings are sequences of UTF8-encoded characters. If you’re a cat lover, you might say “hi ”. Iterate over this string and print each character, codepoint, and byte respectively. Consult the docs[29] for available methods. What’s the unicode codepoint for the Chinese character for cat, and how many bytes does this character take?

Use "each" Methods For Performance

images/aside-icons/tip.png

Use the each methods for iteration: They’re much faster than a simple while loop or looping over a string by indexing with [].

c. object_id

Show that two constant strings with the same value reference the same object on the heap. (Hint: Use the object_id method.) Why would Crystal implement it this way? In general, when two objects have the same object_id, the operator == will return true, as will the method same?. What happens when you ask for the object_id of an integer or a Boolean?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.137.67