Class: String
A string is a sequence of UTF-8 characters enclosed betweend double quotes.
Up to now no special characters have been introduced (like ‘\n’ or similar) nor particular string options like string interpolation, but they’re planned to be implemented in future.
Users must be careful with method aliases: this type has methods
which modify the content of a sring, and others which return a new one.
The first ones usually end with !
, but there are some exceptions
like String#[]=
Defined in:
String.cr
Index:
- #*
- #+
- #==
- #[]
- #[]=
- #chars
- #clone
- #compact
- #compact!
- #concat
- #each
- #gsub
- #hash
- #include?
- #init
- #insert
- #lowcase
- #lowcase!
- #size
- #split
- #starts_with?
- #to_f
- #to_i
- #to_sym
- #upcase
- #upcase!
str * integer -> new_string
Performs a multiplication between a string and a number
bark := "Bark"
bark_3 := bark * 3 #=> "BarkBarkBark"
str + other_str -> new_str
Adds two strings. This method can be invoked in two ways:
foo := "Foo"
bar := "Bar"
foobar := foo + bar #=> "FooBar"
str == other -> boolean
Compares two strings or a string with another object
bar := "Bar"
foo := "Foo"
bar == bar #=> true
bar == foo #=> false
bar == 2 #=> false
str[index] -> string or null
str[range] -> string
Access the string characters at the given index
str := "A quite long string"
str[0] #=> "A"
str[8] #=> "l"
str[2..6] #=> "quite"
str[index] = string -> str
Sets a char or a set of chars in a specified index
a := "Gun"
a[0] := "F" #=> "Fun"
a[2] := "fair" #=> "Funfair"
a[8] := "!" #=> Raises an error
chars() -> array
Returns an array containing each char of the string
"abc".chars() #=> ["a","b","c"]
clone() -> new_str
Clones a string
a := "Foo"
b := a # b and a are pointing to the same object
# Now b and c are going to point to two different objects
c := a.clone() #=> "Foo"
compact() -> new_str
Clones the string and deletes the spaces on this last one
"Compacting This String".compact() #=> "CompactingThisString"
compact!() -> str
Deletes the spaces of str
"Compacting This String".compact!() #=> "CompactingThisString"
concat(str1,str2,str3…) -> str
Concatenates other strings at the given one
a := "1"
a.concat("2") #=> "12"
a.concat("3","4") #=> "1234"
each(&block) -> str
Iterates over each char of the string
"abcd".each_char() { (chr)
print "Char: ",chr
printl
}
#=> Char: a
#=> Char: b
#=> Char: c
#=> Char: d
gsub(pattern,replacement) -> new_str
Returns a new string where every occurrence
of pattern
replaced with the content in replacement
"comfort".gsub("o","*") #=> c*mf*rt
"comfort".gsub("com","ef") #=> effort
hash() -> integer
Return string hash based on length and content
include?(string) -> boolean
Checks if a substring is contained in another one. It works making a call like this:
str := "A cat on the roof"
cat := "Cat"
str.include(cat) #=> true
str.include("bed") #=> false
init(string) -> new_string
Initializes a new string through the keyword ‘new’ or just assigning it. This is the ‘init’ method of the class
str := "Foo" #=> Foo
str := new String("Foo") #=> Foo
insert(index,string) -> str
Inserts a second string in the current one
a := "0234"
a.insert(1,"1") #=> "01234"
a.insert(5,"5") #=> "012345"
a.insert(7,"6") #=> Raises an error
lowcase() -> new_string
Performs the downcase on the whole string without overwriting the original one
"FOO.lowcase() #=> "foo"
lowcase!() -> str
Performs the downcase on the whole string overwriting the original one
"FOO.lowcase!() #=> "foo"
size() -> integer
length() -> integer [alias]
Returns the string size
a := "Hello, world"
a.size() #=> 12
split(delimiter := “ “) -> array
Splits a string according to a specific delimiter, returning an array.
If delimiter
is not specified, a white space will be used
a := "a,b,c,d"
a.split(",") #=> ["a","b","c","d"]
starts_with?(str_beg) -> boolean
Returns true if the beginning of str
matches str_beg
,
false in all the other cases
"hola".starts_with? ("h") #=> true
"hola".statrs_with? ("ho") #=> true
to_f() -> float
Converts a string into a float number
"12".to_f() #=> 12.0
"12.24".to_f() #=> 12.24
"12.ab".to_f() #=> 12.0
"abcd".to_f() #=> 0.0
to_i() -> integer
Converts a string into an integer number. Warning: no overflow is checked yet
"12".to_i() #=> 12
"12x".to_i() #=> 12
"abcd".to_i() #=> 0
to_sym() -> symbol
Converts str
into a symbol
"foo".to_sym() #=> :foo
"32".to_sym() #=> :"32"
upcase() -> new_string
Performs the upcase on the whole string without overwriting the original one
"foo".upcase() #=> "FOO"
upcase!() -> str
Performs the upcase on the whole string overwriting the original one
"foo".upcase!() #=> "FOO"