Ruby: String Classes
Tags: ruby, programming, beginners, learning
Published: 2024-02-22 23:13:04


In Ruby, string classes are used to represent and manipulate text data. This data can be anything inside of parenthesis. Therefore, if a number is represented inside of a parenthesis, such as "25", then it is no longer a digit but rather text in a string. 

Ruby provides several built-in classes and methods for working with strings. Here are some key aspects of string classes in Ruby:

**String Literal**: Strings in Ruby can be created using single quotes (') or double quotes ("). Double-quoted strings allow for interpolation and escape sequences, while single-quoted strings are more literal.

**String Interpolation**: Double-quoted strings allow for embedding Ruby expressions within them using #{} syntax.

```
name = "Alice"
age = 30
profession = "software engineer"

# Using string interpolation to embed variables within a string
message = "Hello, my name is #{name}. I am #{age} years old, and I work as a #{profession}."

puts message
```

**String Concatenation**: Strings can be concatenated using the + operator or the << method.

```
str1 = "Hello"
str2 = "World"
combined = str1 + ", " + str2  # returns "Hello, World"
```

### String class Methods

Methods are blocks of reusable code that perform a specific task. They encapsulate functionality, making it easier to organize and manage code. Methods can take input parameters, perform operations based on those parameters, and optionally return a result.

When using a method in ruby, a period will go before the method.
The period (.) before a method is used to indicate that the method being called is being invoked on a specific object. This notation is known as "dot notation" or "method chaining."

String class methods are methods that are defined on the String class in a programming language like Ruby. These methods are used to perform various operations on string objects.

Below are examples of string class methods:

**.empty?:** checks if the string is empty; will return true if string is empty

**.include?():** Searches for a substring

**.downcase:** returns a copy of the String with all uppercase letters replaced with their lowercase counterparts

**.swapcase:** returns a copy of the String with all uppercase letters replaced with their lowercase counterparts, and vice versa

**.upcase:** returns a copy of the String with all lowercase letters replaced with their uppercase counterparts

**.reverse:** returns a new String with the characters from the String in reverse order

**.concat:** can accept an integer as an argument, which it interprets as an ASCII code, translates into a single character, and adds to the original string

**.gsub():** returns a copy of the String it was called on with all occurrences of the first argument substituted for the second argument

**.strip:** removes all leading and trailing whitespace

**.capitalize:** returns a String with the first character converted to uppercase and the remainder to lowercase

**gets.chomp:** gets input from user & newline by default ; can use gets if no new line is needed

**.split:** will divide the string where it sees that bit of text; called a delimiter

**.length:** checks for the amount of characters in string

**.start_with?() / .end_with?():** Checks if a string contains specific prefix and suffix

**.delete():** Deletes specific characters from a string.

Happy Coding!

theGlamTechie