Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions lib/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,40 @@ instructor_notes: Feel free to re-organize the headings (or add/remove headings)

# What is a String?

Replace me with your response...
A string, simply put, is a collection of special characters, letters, and/or numbers. Strings can be as a simple as a name to something longer like an entire sentence or paragraph.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Within Ruby Strings, letters, numbers, and special characters are all technically thought of as "characters".

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I keep thinking in terms of datatype, but thanks for clarifying.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're welcome!



# What are some examples of information that would be Strings as opposed to some other data type?

Replace me with your response...
Since strings are unique in that they can contain a variety of data types like numbers, spaces, letters, and/or special characters, something like a street address (ex: "1132 E. 22nd St.") would be considered a string since it contains, letters, numbers, and special characters.


# What is one way, using Ruby, to retrieve the 6th character in a String like `"Ada Lovelace"`? How about the 8th character? What happens if you try to retrieve the value of the _99th_ character (Or any character that doesn't exist)?

Replace me with your response...
Steps to retrieve the 6th character in "Ada Lovelace":
1) string1 = "Ada Lovelace"
2) string1[5..5]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The command string_name[x..n] is typically used to return a range/selection. How might you simplify the command, since we are just returning one character?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could jut use the literal itself to grab the 6th character:

"Ada Lovelace"[5..5]

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! You definitely could. I don't mind your variable use, actually. I'm more concerned about the values within the brackets.

With that in mind, how might you simplify string1[5..5]?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could just type in the one position I'm looking for to return its value, like so:

string1[5]

3) Returns "o"

Code to retrieve the 8th character in "Ada Lovelace":
1) string1 = "Ada Lovelace"
2) string1[7..7]
3) Returns "e"

Steps to retrieve the 99th character in "Ada Lovelace":
1) string1 = "Ada Lovelace"
2) string1[99..99]
3) Returns nil



# The previous question asks about finding, for example, the 6th character in a String. Is it possible to find the **-6th** (Notice the negative symbol!) character in a String? What does that even mean?

Replace me with your response...
It means when searching for the specified substring, the program will count from the end of the string backwards, with the last position of the string being -1 (since there's no such thing as a -0).


# What is one way, using Ruby, to replace certain characters in a string with some other set of characters? For example, given `"Sumeet Jain"`, how would you replace all of the `e` characters in my name with exclamation marks? (So it would be `"Sum!!t Jain"`.)

Replace me with your response...
Code to replace the "e" in Sumeet's name with an exclamation point ("!"):
1) name1 = "Sumeet Jain"
2) name1.gsub("e","!")