[Tut] Fun with Strings - Printable Version +- Support Forums (https://www.supportforums.net) +-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87) +--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18) +---- Forum: Ruby and Ruby on Rails (https://www.supportforums.net/forumdisplay.php?fid=55) +---- Thread: [Tut] Fun with Strings (/showthread.php?tid=4342) |
[Tut] Fun with Strings - Jordan L. - 01-14-2010 Hey there. Today I'm going to tell you about strings, and the fun things you can do with them. First, I'll try to explain what a string is by giving some examples and a quote from Google. Google Wrote:a collection of objects threaded on a single strandI know the above quote is to do with things irl, but it's pretty much the same for Programming in Ruby. A string simply is a line of code with objects. Example: "This is a string" 1. Reversing a String. There's a simple way to reverse a string, printing it out backwards in Ruby. For example, here is a code that gets the User's Number / Word and prints it out reversed. Code: ############################################################################ Really the only code in there you may or may not know is the num.reverse. Each time a User inputs sometihng it is automatically put into what we call strings. If we were to write: Code: puts "Enter a Number or Word: " Here's my example from the program I made: My Script Wrote:jordan@ModernWarfare ~/Desktop/Ruby $ ruby strings.rb 2. Uppcasing and Lowercasing Doing an upper case and a lower case is no different to doing a reverse. Just simple replace .reverse with either .upcase or .lowcase Example: Code: puts "Enter a Word: " Where as: Code: puts "Enter a Word: " Thanks for reading my tut on fun with Strings, and I hope it helped you learn something. Thanks! -Jordan. RE: [Tut] Fun with Strings - Gaijin - 01-14-2010 Nice one man, really good job! Do you know something about Regular Expressions? Or how to replace parts of strings. RE: [Tut] Fun with Strings - Jordan L. - 01-14-2010 I'm not sure about regular expressions, but I heard my mate is making a tut. And for replacing in a strign, I think it's something using GSUB. Like; Code: puts "Jordan".gsub("J", "X") I think that's how it works anyway, I don't know, try it. Mark (Wolskie) taught me that RE: [Tut] Fun with Strings - Gaijin - 01-14-2010 (01-14-2010, 07:44 PM)trilobyte- Wrote: I'm not sure about regular expressions, but I heard my mate is making a tut. Yes that worked, thanks! And about the regular expressions, they work with the same function... This code will convert every letter lowercase and uppercase from a-z with to the number 0... Code: puts "Jordan".gsub(/[A-Za-z]/, "0") RE: [Tut] Fun with Strings - Jordan L. - 01-14-2010 Ah, I see what you mean. Thanks for that, I didn't know that So if I was to do: Code: puts "Jordan".gsub(/A]/, z) Or sometihng like that, it would convert the capital J to a lowercase? RE: [Tut] Fun with Strings - Gaijin - 01-14-2010 (01-14-2010, 08:07 PM)trilobyte- Wrote: Ah, I see what you mean. You would use gsub(/[J]/, "j") to do that... Your buddy has posted a tut, go and learn it RE: [Tut] Fun with Strings - Jordan L. - 01-14-2010 Will do, thanks MoTU. RE: [Tut] Fun with Strings - Gaijin - 01-17-2010 I'm back with a question.... How can I use defined variables inside of a String? Something like this in PHP: PHP Code: echo $hello." World"; |